zabbix API json 请求 python urllib.request

zabbix API json request with python urllib.request

我正在处理我的 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,我遇到了图书馆提出的许多错误。检查我做了什么('data' 内的请求相同):

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) 只是将我的 array/dictionnary 转换成一个字符串,这对 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

事实上,您只需要像这样将 json 数据作为字节序列传递:

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 data should be bytes, an iterable of bytes, or a file object. It cannot be of type 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() returns 一个字符串,因此你必须调用 json.dumps().encode() 将其转换为字节数组。

顺便说一下,当您想要转换将作为 url 参数传递的字符串时,使用 urlencode(i.e:转换空格字符到“%20”)。该方法输出的是字符串,不是字节数组