Unpacking msgpack from respond in python

房东的猫 提交于 2020-04-11 08:49:19

问题


I get the following error while trying msgpack.unpack:

ExtraData: unpack(b) received extra data.

Part of my code:

r1=requests.get('http://localhost:3000/fs?path='+r.json()['object'])
unp = msgpack.unpackb(r1.content)

Can someone help with that?


回答1:


The doc's aren't very clear about this, but msgpack.unpackb is a "one-shot" unpacker - you can't give it a larger stream with extra data in it. I assume that you are getting multiple msgpack objects and you can read them with msgpack.Unpacker as in

r1=requests.get('http://localhost:3000/fs?path='+r.json()['object'])
for unp in msgpack.unpackb(r1.content):
    do something...

The reason for this is that the msgpack deserializer reads data in chunks for greater efficiency. For unpackb, where you can only return one object, its chunk reader consumed more of the data stream than it should have and you lost data.



来源:https://stackoverflow.com/questions/42907315/unpacking-msgpack-from-respond-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!