gpt4 book ai didi

python - python + json 与curl 的问题

转载 作者:太空宇宙 更新时间:2023-11-03 16:18:45 24 4
gpt4 key购买 nike

所以当我运行 python 代码时,服务器(谷歌)给我的响应与我运行 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 返回的位置不同。我没有收到错误代码,因此我认为语法是正确的。

最佳答案

数据已经是一个 JSON 编码字符串,您不想对其进行两次编码。

将其传入而不再次编码:

jWifiReport = urllib2.urlopen(req, jWifi).read()

如果您有 Python 数据结构(本例中为字典),则只需进行编码。

关于python - python + json 与curl 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38674459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com