我正在处理我的python项目,并且从python2.6迁移到python 3.6。所以我不得不用urllib.request(以及.error和.parse)替换urllib2。
但是我面临一个我无法解决的问题,这就是...
我想发送一个用JSON编写的请求,如下所示:
import json
import urllib2
data= json.dumps({
"jsonrpc":"2.0",
"method":"user.login",
"params":{
"user":"guest",
"password":"password"
}
"id":1,
"auth":None
})
使用urllib2我没有遇到任何问题,我只需要使用以下命令创建请求:
req=urllib2.Request("http://myurl/zabbix/api_jsonrpc.php",data,{'Content-type':'application/json})
发送:
response=urllib2.urlopen(req)
很好,但是现在有了urllib.request,我遇到了库引发的许多错误。检查我做了什么(请求在“数据”中是相同的):
import json
import urllib.request
data= json.dumps({
"jsonrpc":"2.0",
"method":"user.login",
"params":{
"user":"guest",
"password":"password"
}
"id":1,
"auth":None
})
req = urllib.request.Request("http://myurl/zabbix/api_jsonrpc.php",data,{'Content-type':'application/json})
response = urllib.request.urlopen(req)
我得到这个错误:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/tmp/Python-3.6.1/Lib/urllib/request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "/tmp/Python-3.6.1/Lib/urllib/request.py", line 524, in open
req = meth(req)
File "/tmp/Python-3.6.1/Lib/urllib/request.py", line 1248, in do_request_
raise TypeError(msg)
TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
因此,我对此进行了询问,并了解到必须使用urllib.parse.urlencode()函数将请求转换为字节,因此我尝试在请求中使用它:
import urllib.parse
dataEnc=urllib.parse.urlencode(data)
发生另一个错误:
Traceback (most recent call last):
File "/tmp/Python-3.6.1/Lib/urllib/parse.py", line 842, in urlencode
raise TypeError
TypeError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/tmp/Python-3.6.1/Lib/urllib/parse.py", line 850, in urlencode
"or mapping object").with_traceback(tb)
File "/tmp/Python-3.6.1/Lib/urllib/parse.py", line 842, in urlencode
raise TypeError
TypeError: not a valid non-string sequence or mapping object
并且我意识到json.dumps(data)只是将我的数组/字典转换为字符串,这对于urllib.parse.urlencode函数无效,所以我从数据中删除了json.dumps并执行了以下操作:
import json
import urllib.request
import urllib.parse
data= {
"jsonrpc":"2.0",
"method":"user.login",
"params":{
"user":"guest",
"password":"password"
}
"id":1,
"auth":None
}
dataEnc=urllib.parse.urlencode(data) #this one worked then
req=urllib.request.Request("http://myurl/zabbix/api_jsonrpc.php",data,{'Content-type':'application/json})
response = urllib.request.urlopen(req) #and this one too, but it was too beautiful
然后我看看响应并得到了这个:
b'{"jsonrpc":"2.0",
"error":{
"code":-32700,
"message":"Parse error",
"data":"Invalid JSON. An error occurred on the server while parsing the JSON text."}
,"id":1}
我想这是因为JSON消息不是json.dumped!
总有一个因素阻止我正确执行请求,
因此,如果您有任何想法或替代方案,我将非常高兴,我对此一无所知。
最好的祝福
Gozu09
最佳答案
实际上,您只需要像这样将字节数据作为字节序列传递即可:
data= {
"jsonrpc":"2.0",
"method":"user.login",
"params":{
"user":"guest",
"password":"password"
}
"id":1,
"auth":None
}
req = urllib.request.Request(
"http://myurl/zabbix/api_jsonrpc.php",
data=json.dumps(data).encode(), # Encode a string to a bytes sequence
headers={'Content-type':'application/json}
)
POST数据应为字节,字节的可迭代数或文件对象。不能是str类型
此错误意味着
data
参数应该是字节的可迭代项。st = "This is a string"
by = b"This is an iterable of bytes"
by2 = st.encode() # Convert my string to a bytes sequence
st2 = by.decode() # Convert my byte sequence into an UTF-8 string
json.dumps()
返回一个字符串,因此您必须调用json.dumps().encode()
将其转换为字节数组。顺便说一下,当您要转换将作为url参数传递的字符串时(即:将空格字符转换为“%20”),将使用urlencode。此方法的输出是字符串,而不是字节数组