python - 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 个缓冲区?
解决方案
所以我不得不改变
"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字节)需要它
推荐阅读
- opencv - 如何创建具有特定类型的 Emgu::CV::Image?
- python - 在 matlab 引擎中运行 start_matlab 时出现分段错误
- list - 如何从haskell中的列表中排除特定的元组
- c# - 如何在 C# 中为以下 JSON 值创建属性?
- ruby-on-rails - 在 Ruby 中调用类的实例方法的原因?
- typescript - 如何递归检查Firebase中是否存在节点?
- vue.js - 如何动态加载 vue-form-generator 模式?
- artifactory - 工件是否为 docker 注册表提供基于证书的身份验证
- python - 无法包含外部 pandas 文档 Pycharm v--2018.1.2
- sql-server - 在 MS SQL Server 上使用休眠模式的基于模式的多租户