Python。类、结构、字典?

发布于 2024-10-17 07:08:32 字数 98 浏览 14 评论 0原文

我正在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弥繁 2024-10-24 07:08:32

你应该使用一个类。它为您未来的成长提供了最大的灵活性。

字典具有更详细的语法来访问其内容,并且不允许您在需要时添加方法。

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.

萌辣 2024-10-24 07:08:32

我非常喜欢这样的字典,因为

  1. 你可以做这样的事情Packet['srcPort']
  2. 然后你可以将你的数据包推入MongoDB之类的东西中并稍后以很酷的方式查询它们(即给我所有进入的数据包)端口 80,来自某个设置了特定标志的子网)

Im a big fan of dictionaries for things like this because

  1. You can do things like thisPacket['srcPort']
  2. You can then just shove your packets into something like MongoDB and query them later in cool ways (i.e. give me every packet that came in on port 80 and was from some subnet with a particular flag set)
无人问我粥可暖 2024-10-24 07:08:32

我非常喜欢详细的要求。

缺少这些,我会选择一个命名元组。

您可以执行诸如 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文