【问题标题】:struct.unpack() struct.error: unpack requires a buffer of 124 bytesstruct.unpack() struct.error: unpack 需要 124 字节的缓冲区
【发布时间】:2021-10-06 14:37:43
【问题描述】:

我有一个套接字客户端与 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 个缓冲区?

【问题讨论】:

    标签: python struct


    【解决方案1】:

    python struct unpack length error

    所以我不得不改变

    "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 unpack length error

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-24
      • 2020-10-24
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多