【发布时间】:2017-01-15 19:37:43
【问题描述】:
我是 python 和 xml-rpc 的新手,我一直在解码来自公共服务的二进制数据:
带有此代码的服务请求响应是:
from xmlrpc.client import Server
import xmlrpc.client
from pprint import pprint
DEV_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
logFile = open('stat.txt', 'w')
s1 = Server('http://muovi.roma.it/ws/xml/autenticazione/1')
s2 = Server('http://muovi.roma.it/ws/xml/paline/7')
token = s1.autenticazione.Accedi(DEV_KEY, '')
res = s2.paline.GetStatPassaggi(token)
pprint(res, logFile)
回复:
{'id_richiesta': '257a369dbf46e41ba275f8c821c7e1e0',
'risposta': {'periodi_aggregazione': <xmlrpc.client.Binary object at 0x0000027B7D6E2588>,
'tempi_attesa_percorsi': <xmlrpc.client.Binary object at 0x0000027B7D9276D8>}}
我需要解码这两个二进制对象,但我被这段代码卡住了:
from xmlrpc.client import Server
import xmlrpc.client
from pprint import pprint
DEV_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxx'
logFile = open('stat.txt', 'w')
s1 = Server('http://muovi.roma.it/ws/xml/autenticazione/1')
s2 = Server('http://muovi.roma.it/ws/xml/paline/7')
token = s1.autenticazione.Accedi(DEV_KEY, '')
res = s2.paline.GetStatPassaggi(token)
dat = xmlrpc.client.Binary(res)
out = xmlrpc.client.Binary.decode(dat)
pprint(out, logFile)
结尾是:
Traceback(最近一次调用最后一次):文件“stat.py”,第 18 行,在 dat = xmlrpc.client.Binary(res) 文件 "C:\Users\Leonardo\AppData\Local\Programs\Python\Python35\lib\xmlrpc\client.py", 第 389 行,在 init 中 data.class.name) TypeError: expected bytes or bytearray, not dict
我为 xmlrpc.client 找到的唯一文档是 docs.python.org 的文档,但我不知道如何解码这些二进制文件
【问题讨论】:
-
看来您的调用是正确的,也许问题出在网站上? 'TypeError:预期的字节或字节数组,而不是字典'?我认为您实际上收到的是“dict”而不是“bytearray”
-
是的,我认为这是一个字典。我会尝试联系网站开发者
标签: python python-3.x xml-rpc xmlrpclib xmlrpcclient