Python。类、结构、字典?
我正在将 TCP/IP 数据包分解为字符串格式。构建和存储它的最佳方式是什么?我应该将它存储为 ctypes 结构、python 类、字典还是其他方式吗?每种方法的优点和缺点是什么?
I am disassembling TCP/IP packet into string format. What is the best way to structure and store it? Should I store it as a ctypes struct, python class, dictionary, or some other way? What are the pros and cons of each method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你应该使用一个类。它为您未来的成长提供了最大的灵活性。
字典具有更详细的语法来访问其内容,并且不允许您在需要时添加方法。
ctypes 实际上用于访问打包用于其他 API 的结构。如果你的数据保留在 Python 中,唯一的好处是它在内存中会更加紧凑。
You should use a class. It gives you the most flexibility to grow in the future.
Dictionaries have more verbose syntax to access their contents, and don't allow you to add methods if you need them.
ctypes is really for accessing structures packed for use in other APIs. If your data is going to stay in Python, the only benefit is that it will be more compact in memory.
我非常喜欢这样的字典,因为
Im a big fan of dictionaries for things like this because
我非常喜欢详细的要求。
缺少这些,我会选择一个命名元组。
您可以执行诸如
thisPacket.srcPort
之类的操作。然后,您可以将数据包放入 MongoDB 之类的东西中,然后以很酷的方式查询它们
I'm a big fan of detailed requirements.
Lacking those, I'd go with a named tuple.
You can do things like
thisPacket.srcPort
.You can then just shove your packets into something like MongoDB and query them later in cool ways