1
所以當我運行Python代碼時,服務器(google)給了我一個與運行curl命令不同的響應。有人能告訴我我錯在哪裏嗎?python + json與curl的問題
代碼:
import urllib2, simplejson
def MapsWIFI(card):
req = urllib2.Request("https://www.googleapis.com/geolocation/v1/geolocate?key=AI...")
jWifi = """
{
"wifiAccessPoints": [
{
"macAddress": "64:D1:A3:0A:11:65",
"channel": 6,
},
... #some AP here
]
}
"""
print jWifi
req.add_header("Content-Type", "application/json")
jWifiReport = urllib2.urlopen(req,simplejson.dumps(jWifi)).read()
print jWifiReport
APdetected = str(len(wifiCell))
mapsDict = simplejson.loads(jWifiReport)
location = str(mapsDict.get("location",{}))[1:-1]
accuracy = "Accuracy: "+str(mapsDict.get("accuracy",{}))[1:-1]
mapMe = "|---"+location.split(",")[0]+"\n|---"+location.split(",")[1][1:]+"\n|---$
return mapMe
MapsWIFI("wlp8s0")
和命令是:
curl -d @file2.json -H "Content-Type: application/json" -i "https://www.googleapis.com/geolocation/v1/geolocate?key=AI..."
其中file2.json正好包含jWifi該格式。 問題是,如上所述,代碼返回的位置與curl返回的位置不同。我沒有得到錯誤代碼,所以我認爲語法是正確的。
出於興趣,爲什麼要使用'simplejson'庫? Python 2.7帶有'json'模塊,它與*完全相同的項目*。 –
是的,你是對的。但是這是一個旨在儘可能便攜的項目的一部分,所以這應該與其他版本兼容。 – Pielco11