将从网络数据包接收的字符串数据转换为Structure

时间:2013-06-14 21:55:02

标签: python c ctypes

我是python和ctypes的新手。我所拥有的是: -

C程序:

struct query
{
  uint16_t req_no;
  uint32_t req_len;
  uint64_t req;
};

struct response
{
  uint16_t req_no;
  uint16_t status;
  uint32_t value_len;
  uint64_t value;
};

// functions for creating query and response packets using
// above structs respectively, returning char buffer.
char* create_query(//some args);
char* create_response(//some args);

我为上面的C代码创建了一个libquery.so。我的TCP服务器是一个C程序。 我正在尝试创建一个TCP python客户端(我的项目需要它!)。

我可以从python客户端成功发送查询和接收数据(使用libquery.so中的函数)。 但是当我得到响应数据时,我想将其转换为“struct response”类型。 我在python中创建了一个类似的“Structure”类,但是无法从中获取任何东西。

请帮忙。

我的Python代码的一些代码片段: -

// some ctypes imports 
lib = cdll.LoadLibrary('./libquery.so')

class Info1(Structure):
    _fields_ = [("req_no",c_int),
                ("status",c_int),
                ("value_len",c_int),
                ("value",c_int)]

header = Info1()

// Did some TCP connection code here and send data to server by calling
// create_query() method, data confirmed to be correct on server side...

# Receive response data
data = sock.recv(512)
header = str_to_class('Info1')

header.req_no    =  int(ord(data[0])) // Works; but I don't want to go this ways..
header.status    =  int(ord(data[1]))
header.value_len =  int(ord(data[2]))
header.value     =  int(ord(data[3]))

print above header values..

我尝试使用: -

 def str_to_class(Info1):
    return getattr(sys.modules[__name__], Info1)

但不知道如何让它发挥作用。

任何人都知道如何使它工作或者还有其他方法吗?

1 个答案:

答案 0 :(得分:2)

您的'Info1'与C'结构响应'不匹配。所以我改变了以下代码。 您可以使用ctypes.memmove

from ctypes import *

class Info1(Structure):
    _fields_ = [("req_no", c_uint16),
                ("status", c_uint16),
                ("value_len", c_uint32),
                ("value", c_uint64)]

data = (
    '\x01\x00'
    '\x02\x00'
    '\x03\x00\x00\x00'
    '\x04\x00\x00\x00\x00\x00\x00\x00'
)
# Assumed data was received. I assumed both server, clients are little-endian.
# else, use socket.ntoh{s|l}, socket.hton{s|l} ....

header = Info1()
memmove(addressof(header), data, sizeof(header))
assert header.req_no == 1
assert header.status == 2
assert header.value_len == 3
assert header.value == 4

您也可以使用struct

import struct
data = '....' # same as above
struct.unpack('HHLQ', data) == (1, 2, 3, 4) # '>HHLQ' if data is htonl/htons-ed in sencding part.
相关问题