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?
Try this — works on 2.7 and 3.2.
Script:
Output: