1

I'm new to python and xml-rpc , and I'm stuck with decoding binary data coming from a public service :

the service request response with this code is :

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)

response :

{'id_richiesta': '257a369dbf46e41ba275f8c821c7e1e0',
 'risposta': {'periodi_aggregazione': <xmlrpc.client.Binary object at 0x0000027B7D6E2588>,
              'tempi_attesa_percorsi': <xmlrpc.client.Binary object at 0x0000027B7D9276D8>}}

I need to decode these two binary objects , and I'm stuck with this code :

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)

that ends in :

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

The only doc I found for xmlrpc.client is the one at docs.python.org , but I can't figure out how I could decode these binaries

  • 1
    It seems like you are doing the correct call, maybe the problem is on the website? ´TypeError: expected bytes or bytearray, not dict´ ? I would think that you are actually receiving a ´dict´ instead of a ´bytearray´ – NeoVe Sep 08 '16 at 01:37
  • 1
    yes , I think it's a dict. I'll try to contact website developer – Forzaferrarileo Sep 10 '16 at 15:50

1 Answers1

0

If the content of res variable (what you get from the 2nd (s2) server) is the response you pasted into the question, then you should modify the last 3 lines of your 2nd snippet to (as you already have 2 Binary objects in the res dictionary):

# ... 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)

Notes:

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

  • I wasn't able to connect (and this test the solution), since DEV_KEY is (obviously) fake
CristiFati
  • 38,250
  • 9
  • 50
  • 87