Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5846971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:39:08+00:00 2026-05-22T12:39:08+00:00

I have started to write a Python 3.x client application. The server application exists

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:39:09+00:00Added an answer on May 22, 2026 at 12:39 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started coding an FTP client application (for fun). I’m trying to represent
I'm an avid vim user and have started to write some SQL code recently.
We have started to use spring aop for cross cutting aspects of our application
I have started using Jython as it seems to be a excellent language, and
I have started using Linq to SQL in a (bit DDD like) system which
I have started using aspects for parameter validation in our development framework. It works
I have started to play a little with Qt 4. And then I have
I have started testing my UI using qUnit, so I need to simulate some
We have started using Spring framework in my project. After becoming acquainted with the
I have started working on some code left behind by previous developers, and I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.