How it works...
First of all, we opened the file in binary mode (the rb argument). This tells Python to avoid trying to decode the contents of the file as if it was text; the content is returned as it is in a bytes object.
Then the data we read with f.read() is passed to struct.unpack_from, which is able to decode binary data as a set of numbers, strings, and so on. In our case, we used > to specify that the data we are reading is in big-endian ordering (like all network-related data) and then HHLL to state that we want to read two unsigned 16-bit numbers and two unsigned 32-bit numbers (the ports and the sequence/acknowledgment numbers).
As we used unpack_from, any other remaining data is just ignored after the four specified numbers are ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access