Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python. Class, Struct, Dictionary?

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?

like image 657
PSS Avatar asked May 03 '26 04:05

PSS


2 Answers

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.

like image 136
Ned Batchelder Avatar answered May 05 '26 18:05

Ned Batchelder


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)
like image 22
Alex Avatar answered May 05 '26 19:05

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!