Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to elegantly send/receive a large C struct using Python?

I have started to write a Python 3.x client application. The server application exists already and is written in C. The server provides a C header file with the definition of two structures used to send and receive data via UDP (I am using Python's socket module). The problem is that the C structures are quite large (around 200 elements each). If I use Python's struct module to pack/unpack the data, a not-so-elegant solution would be packing/unpacking the 200 elements manually, like:

struct.pack('H...I', data1, ..., data200)

Furthermore, I want to be able to access the received/sent elements in Python using a C-like syntax. For example, if I do in the C server side

send.data.pos = pos;

it would be nice (most natural) if I can access the pos variable in the Python client side like this:

pos = recv.data.pos

Note that the question is not how to automatically write the structure in Python from the header file, like in this thread (I have no problem in writing each structure field one by one in Python), but rather what would be the best way to organise the data in Python (e.g. in classes, using dictionaries, etc.) that will allow me to exploit Python features and made the code simpler and the data easy to access (I'd rather use only Python standard modules, no external software). What would be the most elegant way to achieve this?

like image 777
jpmz Avatar asked Jul 28 '26 19:07

jpmz


1 Answers

Try this -- works on 2.7 and 3.2.

Script:

import struct, collections

class CStruct(object):

    def __init__(self, typename, format_defn, lead_char="!"):
        self.names = []
        fmts = [lead_char]
        for line in format_defn.splitlines():
            name, fmt = line.split()
            self.names.append(name)
            fmts.append(fmt)
        self.formatstr = ''.join(fmts)
        self.struct = struct.Struct(self.formatstr)
        self.named_tuple_class = collections.namedtuple(typename, self.names)

    def object_from_bytes(self, byte_str):
        atuple = self.struct.unpack(byte_str)
        return self.named_tuple_class._make(atuple)

if __name__ == "__main__":
    # do this once
    pkt_def = """\
        u1 B
        u2 H
        u4 I"""
    cs = CStruct("Packet1", pkt_def)
    # do this once per incoming packet
    o = cs.object_from_bytes(b"\xF1\x00\xF2\x00\x00\x00\xF4")
    print(o)
    print(o.u4)

Output:

Packet1(u1=241, u2=242, u4=244)
244
like image 79
John Machin Avatar answered Jul 30 '26 08:07

John Machin



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!