Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将C结构转换为Python以通过套接字发送_Python_Sockets_Struct_Swig_Ctypes - Fatal编程技术网

将C结构转换为Python以通过套接字发送

将C结构转换为Python以通过套接字发送,python,sockets,struct,swig,ctypes,Python,Sockets,Struct,Swig,Ctypes,我目前正在更新一个用C编写的旧的大型软件,它通过套接字将结构发送到服务器。更新后的软件将用Python编写,这意味着我现在需要以与Python中的C结构相同的字节布局发送相同的信息。我知道Python包结构,并希望使用它。问题是,这个结构绝对是巨大的。给你一个想法,这里有一只鹬: // this is the struct that I wish to send typedef struct { struct st_command_header header; union

我目前正在更新一个用C编写的旧的大型软件,它通过套接字将结构发送到服务器。更新后的软件将用Python编写,这意味着我现在需要以与Python中的C结构相同的字节布局发送相同的信息。我知道Python包结构,并希望使用它。问题是,这个结构绝对是巨大的。给你一个想法,这里有一只鹬:

// this is the struct that I wish to send 

typedef struct
{
    struct st_command_header header;

    union {
        char buffer[32];
        struct set_360_camera_message camera_msg;
        struct u_olcd_msg olcd_msg;
    };   

} struct_to_be_set stc;


// supporting structs

struct st_command_header
{
  union  u_all_types len;
  union  u_all_types cmd;
  union  u_all_types cmd_counter;
  union  u_all_types items;
} __attribute__((packed));

struct u_olcd_msg
{  
  // over 100 lines of fields to fill
}

// all the other structs necessary

据我所知,structpython包要求您手动打包struct,考虑到struct的大小和union语句的数量,这将非常困难。理想情况下,我希望通过某种方式利用现有代码,在C中生成结构,但以某种方式将字节传输到套接字的Python代码中。有办法做到这一点吗?我已经研究了ctypes和swig,但是我没有看到一种从C代码中获取字节表示的简单方法

谢谢JBernardo,这看起来很有帮助。有没有一种简单的方法来访问字节表示法?我也在寻找解决方案。介意分享你的发现吗?谢谢JBernardo,这看起来很有帮助。有没有一种简单的方法来访问字节表示法?我也在寻找解决方案。介意分享你的发现吗?