从xmlrpc python解码二进制文件

2024-05-29 06:04:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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)

回应:

^{pr2}$

我需要解码这两个二进制对象,而我却被这段代码困住了:

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 (most recent call last): File "stat.py", line 18, in dat = xmlrpc.client.Binary(res) File "C:\Users\Leonardo\AppData\Local\Programs\Python\Python35\lib\xmlrpc\client.py", line 389, in init data.class.name) TypeError: expected bytes or bytearray, not dict

我找到的唯一的医生xmlrpc.client是那个在吗docs.python.org,但我不知道如何解码这些二进制文件


Tags: keyfromdevimportclienthttpserverres
1条回答
网友
1楼 · 发布于 2024-05-29 06:04:25

如果res变量的内容(从2nds2)服务器获得的内容是粘贴到问题中的响应,那么您应该将2nd片段的最后3行修改为(因为res字典中已有2个Binary对象):

# Existing code
res = s2.paline.GetStatPassaggi(token)

answer = res.get("risposta", dict())
aggregation_periods = answer.get("periodi_aggregazione", xmlrpc.client.Binary())
timeout_paths = answer.get("tempi_attesa_percorsi", xmlrpc.client.Binary())

print(aggregation_periods.data)
print(timeout_paths.data)

注意事项

  • 根据[Python]: Binary Objects

    Binary objects have the following methods, supported mainly for internal use by the marshalling/unmarshalling code:

  • 我无法连接(这个测试解决方案),因为DEV_KEY(显然)是假的

相关问题 更多 >

    热门问题