0

I have a python utility providing results in JSON through a webserver. The way it is done is by printing "Content-type: text/html\n" and then printing the JSON.dumps result. This work fine and I am able to have a python client opening the URL and loading the JSON as an object with JSON.loads.

Now, I would like to do something similar with msgpack to gain in speed. I tried doing the same process but with msgpack.packb and msgpack.unpackb but the issue is that all the binary values are seen as text and msgpack is unable to deal with that.

For instance, instead of receiving

b"\x82\xa1a\x05\xa1b\x07"

for the object {"a":5,"b":7}, I receive

b'b"\\x82\\xa1a\\x05\\xa1b\\x07"\n'

Is there a standard way to do this? I do not want to open a socket specifically for serving this msgpack data.

1 Answers1

0

I actually found how to do that. You shall use stdout instead of print to keep the msgpack information in binary format:

sys.stdout.buffer.write(b"Content-Type: application/msgpack\n\n")
sys.stdout.buffer.write(msgpack.packb(object))

Now the client just has to call

import msgpack
from urllib.request import urlopen
msgpack.unpack(urlopen("url_calling_above_code"))