反序列化 C 数据
我正在通过网络从 C 服务器接收一个结构到我的 Python 客户端。协议是UDP。我无法控制服务器及其协议/数据格式。它由以下结构组成(是的,一个 IPv4 + 端口):
struct ip_s {
uint8_t i1;
uint8_t i2;
uint8_t i3;
uint8_t i4;
uint16_t port; // big endian
};
除了转换为大端字节序的端口之外,数据“按原样”发送,转换为 (char*)
。
如何将该结构接收为 Python 可处理的格式?
其他信息:
- Python 2.7 或 3.x
- 跨平台
- 最好仅使用内置模块的解决方案
I'm receiving a struct over a network from a C server to my Python client. The protocol is UDP. I have no control over the server and it's protocols/data formats. It consists of this struct (yes, an IPv4 + port):
struct ip_s {
uint8_t i1;
uint8_t i2;
uint8_t i3;
uint8_t i4;
uint16_t port; // big endian
};
Apart from the port which is converted to big-endian the data is sent "as-is", casted to a (char*)
.
How can I receive this struct to a format that is processable by Python?
Misc info:
- Python 2.7 or 3.x
- Cross-platform
- Preferably solution using only built-in modules
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 struct.unpack
它可能看起来像这样:
Look into struct.unpack
It will probably look something like this:
就停在那里。不要那样做。该技术引入了以下依赖性:
(2) 和 (3) 依次取决于:
这是相当多的依赖关系。不要这样做。定义应用程序协议并使用它。或者使用 XDR 之类的工具来解决您的问题。
Stop right there. Don't do that. This technique introduces the following dependencies:
(2) and (3) in turn depend on:
That's rather a lot of dependencies. Don't do this. Define an application protocol and use it. Or use something like XDR that solves the problem for you.