【问题标题】:decode binary from xmlrpc python从 xmlrpc python 解码二进制文件
【发布时间】: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


【解决方案1】:

如果res 变量的内容(您从 2nd (s2) 服务器获得的内容)是您粘贴到问题中的响应,那么您应该修改最后 3 行您的 2nd sn-p 到(因为您在 res dictionary 中已经有 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

    二进制对象有以下方法,主要供内部使用编组/解组代码:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    相关资源
    最近更新 更多