46

这可能是一个愚蠢的问题,但我在文档或任何地方都找不到好的答案。

如果我使用struct定义二进制结构,则该 struct 有 2 种对称的序列化和反序列化方法(打包和解包),但似乎ctypes没有直接的方法来做到这一点。这是我的解决方案,感觉不对:

from ctypes import *

class Example(Structure):
    _fields_ = [
        ("index", c_int),
        ("counter", c_int),
        ]

def Pack(ctype_instance):
    buf = string_at(byref(ctype_instance), sizeof(ctype_instance))
    return buf

def Unpack(ctype, buf):
    cstring = create_string_buffer(buf)
    ctype_instance = cast(pointer(cstring), POINTER(ctype)).contents
    return ctype_instance

if __name__ == "__main__":
    e = Example(12, 13)
    buf = Pack(e)
    e2 = Unpack(Example, buf)
    assert(e.index == e2.index)
    assert(e.counter == e2.counter)
    # note: for some reason e == e2 is False...
4

3 回答 3

36

PythonInfo wiki对此有一个解决方案。

常见问题解答:如何将字节从 ctypes.Structure 复制到 Python?

def send(self):
    return buffer(self)[:]

常见问题解答:如何从 Python 将字节复制到 ctypes.Structure?

def receiveSome(self, bytes):
    fit = min(len(bytes), ctypes.sizeof(self))
    ctypes.memmove(ctypes.addressof(self), bytes, fit)

它们send是(或多或少)等价于pack, 并且receiveSome是一种pack_into. 如果你有一个“安全”的情况,你正在解压到一个与原始结构相同类型的结构中,你可以将它单行 memmove(addressof(y), buffer(x)[:], sizeof(y))复制xy. 当然,您可能会有一个变量作为第二个参数,而不是x.

于 2009-12-01T17:24:38.610 回答
18

看看这个关于 python 中二进制 i/o 的链接:

http://www.dabeaz.com/blog/2009/08/python-binary-io-handling.html

基于此,您可以简单地编写以下内容以从缓冲区(不仅仅是文件)中读取:

g = open("foo","rb")
q = Example()
g.readinto(q)

写起来很简单:

g.write(q)

使用套接字也一样:

s.send(q)

s.recv_into(q)

我用 pack/unpack 和 ctypes 做了一些测试,这种方法是最快的,除了直接用 C 语言编写

于 2009-12-29T00:40:59.023 回答
11

在 Python3 上测试

e = Example(12, 13)
serialized = bytes(e)
deserialized = Example.from_buffer_copy(serialized)
于 2018-05-24T18:41:10.513 回答