Removing C data deserialization

I get the structure through the network from the C server to my Python client. UDP protocol. I do not control the server and its protocols / data formats. It consists of this structure (yes, IPv4 + port):

struct ip_s {
    uint8_t i1;
    uint8_t i2;
    uint8_t i3;
    uint8_t i4;
    uint16_t port; // big endian
};

In addition to the port, which is converted to big-endian, data is sent "as is", discarded to (char*).

How can I get this structure in a format that Python processes?


Additional Information:

  • Python 2.7 or 3.x
  • Cross platform
  • Preferred solution using only built-in modules
+3
source share
2 answers

Take a look at struct.unpack

It probably looks something like this:

# socket setup

(buffer, sockaddress) = mysocket.recvfrom(6)
if len(buffer)== 6:
    i1,i2,i3,i4, port = struct.unpack('!BBBBH', buffer)
+3
source

I get the structure through the network

. . :

  • : big-endian little-endian.
  • .
  • .

(2) (3) :

  • .
  • .
  • #pragmas.
  • , C.

. . . - XDR, .

0

All Articles