1
pythonコードを実行すると、curlコマンドを実行したときとは異なるレスポンスがサーバ(google)に送られます。誰かが私が間違っている場所を教えてもらえますか?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")
、コマンドは次のとおりです。file2.jsonはその形式で正確jWifiが含まれてい
curl -d @file2.json -H "Content-Type: application/json" -i "https://www.googleapis.com/geolocation/v1/geolocate?key=AI..."
。 問題は、コードによって返される場所が、curlによって返される場所と異なることです。私は構文が正しいことを私はエラーコードを取得しないでください。
なぜ、simplejson'ライブラリを使うのですか? Python 2.7には 'json'モジュールが付属しています。これはまったく同じプロジェクト*です。 –
はい、あなたは正しいです。しかし、これは可能な限りポータブルになることを目指すプロジェクトの一部なので、他のバージョンでも動作するはずです。 – Pielco11