admin管理员组文章数量:1611184
我有一个结构(在这种情况下是一个Netlink消息头)我需要通过套接字发送到内核.我弄清楚的唯一方法是使用__reduce __().
>>> class nlmsghdr(ctypes.Structure):
... _fields_ = [('nlmsg_len', ctypes.c_int32),
... ('nlmsg_type', ctypes.c_int16),
... ('nlmsg_flags', ctypes.c_int16),
... ('nlmsg_seq', ctypes.c_int32),
... ('nlmsg_pid', ctypes.c_int32)]
...
>>>
>>> hdr = nlmsghdr(20, 22, 769, 1328884876, 0)
>>> hdr.__reduce__()[1][1][1]
'\x14\x00\x00\x00\x16\x00\x01\x03\x8c,5O\x00\x00\x00\x00'
>>> # socket.send(hdr.__reduce__()[1][1][1])
看起来__reduce__用于序列化(pickle)并且依赖于它总是以相同的方式运行似乎是一个错误.
肯定有更好的办法?
解决方法:
我同意使用__reduce __()感觉不对.
ctypes.string_at(ctypes.addressof(hdr), ctypes.sizeof(hdr))
应该以更透明的方式做到这一点.
标签:python,ctypes,structure
来源: https://codeday.me/bug/20190530/1184503.html
本文标签: 转换为ctypesPythonstrStructure
版权声明:本文标题:python ctypes structure_将Python ctypes.Structure转换为str 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1725839335a1044844.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论