我正在为调用我们的 Rest API 的活动代码生成器编写一个 python 语言插件。在多次尝试使用 requests 库但失败后,我选择使用更低级别的 socket 和 ssl 模块,这些模块到目前为止运行良好。我正在使用一种非常粗略的方法来解析响应;对于正文中相当短的响应,这很好用,但我现在正在尝试检索更大的 json 对象(用户列表)。响应被截断如下(注意:为简洁起见,我删除了几个用户条目):
{"page-start":1,"total":5,"userlist":[{"userid":"jim.morrison","first-name":"Jim","last-name":"Morrison","language":"English","timezone":"(GMT+5:30)CHENNAI,KOLKATA,MUMBAI,NEW DELHI","currency":"US DOLLAR","roles":
在此之后应该有更多用户,并且响应正文在控制台中的一行上。
这是我用来从 Rest API 服务器请求用户列表的代码:
import socket, ssl, json
host = self.WrmlClientSession.api_host
port = 8443
pem_file = "<pem file>"
url = self.WrmlClientSession.buildURI(host, port, '<root path>')
#Create the header
http_header = 'GET {0} HTTP/1.1\n\n'
req = http_header.format(url)
#Socket configuration and connection execution
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn = ssl.wrap_socket(sock, ca_certs = pem_file)
conn.connect((host, port))
conn.send(req)
response = conn.recv()
(headers, body) = response.split("\r\n\r\n")
#Here I would convert the body into a json object, but because the response is
#cut off, it cannot be properly decoded.
print(response)
任何对此问题的见解将不胜感激!
编辑:我忘了提到我在服务器端调试了响应,一切都很正常。