首页 > 解决方案 > struct.unpack() struct.error: unpack 需要 124 字节的缓冲区

问题描述

我有一个与 python 套接字服务器通信的套接字客户端

客户端以 112 字节的块发送数据包,这是我尝试从服务器端解包的方式

self.unpacked = struct.unpack("i f f f f f f f f f i f i f f f f f f B f f B f f B f f B f f", msg)

我有以下错误:

struct.error: unpack requires a buffer of 124 bytes

这是味精的样子:

b'\xcf\xff\xff\xff\x01\x00|D\x07\x01\xc4B\x01\x00\xc8C\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80?P\x00\x00\x00\x00\x00\x00\x00\x00P\x00\x00\x00\x00\x00\x00\x00\x00P\x00\x00\x00\x00\x00\x00\x00\x00P\x00\x00\x00\x00\x00\x00\x00\x00'

它的长度为 112

如果我计算 struct.unpack args 中类型的总大小

我明白了

i f f f f f f f f f i f i f f f f f f B f f B f f B f f B f f
4+4+4+4+4+4+4+4+4+4+4+4+4+4+4+4+4+4+4+1+4+4+1+4+4+1+4+4+1+4+4
== 112 bytes

我的问题是为什么当 args 只指定 112 个字节时 unpack 需要 124 个缓冲区?

标签: pythonstruct

解决方案


python struct解包长度错误

所以我不得不改变

"i f f f f f f f f f i f i f f f f f f B f f B f f B f f B f f"

"<i f f f f f f f f f i f i f f f f f f B f f B f f B f f B f f"

因为“B”(1字节)需要它

python struct解包长度错误


推荐阅读