4
私はStructure
(このケースではNetlink Msgヘッダーです)をソケットを介してカーネルに送る必要があります。私が考え出した唯一の方法は__reduce__()
です。Python ctypes.Structureをstrに変換します
>>> 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__
は(ピクルス)をシリアライズし、いつもと同じようには間違いのように思える機能するために、それに応じてのためにあるように見えます。
もっと良い方法が必要ですか?
もっとよく説明され、回答されました:http://stackoverflow.com/questions/1825715/how-to-pack-and-unpack-using-ctypes-structure-str – tMC