How to pack and unpack using ctypes (Structure <-> str)

后端 未结 3 2248
感情败类
感情败类 2020-11-28 04:45

This might be a silly question but I couldn\'t find a good answer in the docs or anywhere.

If I use struct to define a binary structure, the struct

相关标签:
3条回答
  • 2020-11-28 05:19

    Have a look at this link on binary i/o in python:

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

    Based on this you can simply write the following to read from a buffer (not just files):

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

    To write is simply:

    g.write(q)
    

    The same for using sockets:

    s.send(q)
    

    and

    s.recv_into(q)
    

    I did some testing with pack/unpack and ctypes and this approach is the fastest except for writing straight in C

    0 讨论(0)
  • 2020-11-28 05:22

    The PythonInfo wiki has a solution for this.

    FAQ: How do I copy bytes to Python from a ctypes.Structure?

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

    FAQ: How do I copy bytes to a ctypes.Structure from Python?

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

    Their send is the (more-or-less) equivalent of pack, and receiveSome is sort of a pack_into. If you have a "safe" situation where you're unpacking into a struct of the same type as the original, you can one-line it like memmove(addressof(y), buffer(x)[:], sizeof(y)) to copy x into y. Of course, you'll probably have a variable as the second argument, rather than a literal packing of x.

    0 讨论(0)
  • 2020-11-28 05:29

    Tested on Python3

    e = Example(12, 13)
    serialized = bytes(e)
    deserialized = Example.from_buffer_copy(serialized)
    
    0 讨论(0)
提交回复
热议问题