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

  • SEARCH
  • Home
  • 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 9214061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:53:32+00:00 2026-06-18T01:53:32+00:00

Below Are Struct from C,and i m trying Convert to Python, and use Socket

  • 0

Below Are Struct from C,and i m trying Convert to Python,
and use Socket to sending out the struct

C:

        struct CommandReq
        {
            char    sMark[6];  //start flag\r\n*KW
            short   nPackLen;  //packet length
            short   nFlag;     //command ID 0x0002
            int     nGisIp;    //GIS port
            short   nPort;     //GIS Port
            char    sData[50]; //command string
            char    sEnd[2];   //end flag "\r\n"
        };
        //source code
        CommandReq stResq;
        memset(&stResq, 0, sizeof(stResq));
        sprintf(stResq.sMark, "\r\n%s", "*KW");
        stResq.nFlag = 0x0002;
        stResq.nPackLen = sizeof(stResq);
        stResq.nGisIp = 0;
        stResq.nPort = 0;
        strcpy(stResq.sData, "*KW,CC09C00001,015,080756,#");
        strncpy(stResq.sEnd, "\r\n", 2);

I have created Python struct using namedtuple,and use socket to send this struct.
but unfortunately failed.

Python:

from collections import namedtuple
MyStruct = namedtuple("MyStruct", "sMark nPackLen nFlag nGisIp nPort sData sEnd")

Edit, in reply to answer below

after figure out, python 3.x must be added string .encode(“ascii”)

      format_ = "6shhih50s2s"
      MyStruct = namedtuple("MyStruct", "sMark nPackLen nFlag nGisIp nPort sData sEnd")
      tuple_to_send = MyStruct(sMark="\r\n{}".format("*KW").encode("ascii"), 
                 nPackLen=struct.calcsize(format_),
                 nFlag=0x0002,
                 nGisIp=0,
                 nPort=0,
                 sData= "*KW,NR09G05133,015,080756,#".encode("ascii"),
                 sEnd="\r\n".encode("ascii"))
      string_to_send = struct.pack(format_, *tuple_to_send._asdict().values()
      socket.sendto(string_to_send, self.client_address)

Additional Question

below is the packet are sending out with the struct

format_ = "6shhih50s2s" //total bytes should be 68 bytes?
    0d0a2a4b5700 //6 char \r\n*KW
    4600 // packet length 70 bytes
    0200 //command ID 0x0002,short 2 bytes
    00000000 //GIS port is integer 4 bytes
    0000 //GIS Port short 2 bytes
    0000 //unknown??what is this?
    2a4b572c4e5230394730353133332c3031352c3038303735362c230000000000000000000000000000000000000000000000 //sData[50] char 50 bytes
    0d0a //sEnd 2 char,2 bytes

how come the 2 unknown bytes??

  • 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-06-18T01:53:33+00:00Added an answer on June 18, 2026 at 1:53 am

    namedtuple isn’t really comparable to a c-struct. If you are used to using structs, you can have a look at the struct-module to convert structured information to a string.

    In general, Pythonistas prefer using the pickle-module for serialization.

    from collections import namedtuple
    import pickle # or cPickle, it's faster
    MyStruct = namedtuple("MyStruct", "sMark nPackLen nFlag nGisIp nPort sData sEnd")
    
    tuple_to_send = MyStruct(sMark="abcdef", nPackLen=...)
    string_to_send = pickle.dumps(tuple_to_send)
    

    There are two flavors of pickle, pickle and cPickle. The latter is faster, but is only available in CPython (which most programmers use), while pickle is also available in Jython, IronPython, …

    If you want to stick to struct (e.g., because the other side expects this format), your format string will be

    format_ = "6shhih50s2s"
    

    So you can do:

    import struct
    from collections import namedtuple
    
    format_ = "6shhih50s2s"
    
    MyStruct = namedtuple("MyStruct", "sMark nPackLen nFlag nGisIp nPort sData sEnd")
    tuple_to_send = MyStruct(sMark="\r\n{}".format("*KW"), 
                         nPackLen=struct.calcsize(format_),
                         nFlag=0x0002,
                         nGisIp=0,
                         nPort=0,
                         sData= "*KW,NR09G05133,015,080756,#",
                         sEnd="\r\n")
    
    
    string_to_send = struct.pack(format_, *tuple_to_send._asdict().values())
    

    BTW: namedtuples are immutable, i.e., you cannot do

    tuple_to_send.sMark = "bcdefg"
    

    to change some property. If you need such behavior, you must create a class.

    Edit

    For Python 3, string-treatment has changed. Conversion from unicode to bytes has to be performed, e.g.

    import struct
    from collections import namedtuple
    
    format_ = "6shhih50s2s"
    
    MyStruct = namedtuple("MyStruct", "sMark nPackLen nFlag nGisIp nPort sData sEnd")
    tuple_to_send = MyStruct(sMark="\r\n{}".format("*KW").encode("ascii"), 
                         nPackLen=struct.calcsize(format_),
                         nFlag=0x0002,
                         nGisIp=0,
                         nPort=0,
                         sData= "*KW,NR09G05133,015,080756,#".encode("ascii"),
                         sEnd=b"\r\n")
    
    
    string_to_send = struct.pack(format_, *tuple_to_send._asdict().values())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Python 3.2.3 on Windows, and am trying to convert binary data within
I am trying to represent a HD page in a struct. The code below
I'm trying to save a struct with a char* string into a file. struct
I'm trying to create a struct that can hold generic values. The code below
Im trying to copy substrings from one char* to another, when I printf(%c) it
SLIGHT UPDATE BELOW I am trying to use the [Description] data annotation attribute with
You can see what I'm trying to do below: typedef struct image_bounds { int
I’m trying to use the WTO instruction from with in Metal C to print
I am trying to make a call from python to a dll but am
Goal: I'm trying to use python interactively in my c++ code using Boost::Python library.

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.