In [1]:
from ctypes import *

libc = CDLL('libc.so.6')
print(libc)
print(libc.printf)
<CDLL 'libc.so.6', handle 7fa17976d9c0 at 7fa16c6fb198>
<_FuncPtr object at 0x7fa16fec9c00>
In [2]:
buf = create_string_buffer(50)
print(buf)
libc.sprintf(buf, b'Hello %s\n', b'World')
print(buf.value)
<ctypes.c_char_Array_50 object at 0x7fa16c9b98c8>
b'Hello World\n'
In [3]:
class Test(Structure):
    _fields_ = (
    ('foo', c_int),
    ('bar', c_char))
    
t = Test()
t.foo = 1234
t.bar = b'A'

print(t)

from io import BytesIO

b = BytesIO()
b.write(t)
b.seek(0)
data = b.read()

print(data)
<__main__.Test object at 0x7fa16c9b99d8>
b'\xd2\x04\x00\x00A\x00\x00\x00'
In [4]:
b = BytesIO(data)
t2 = Test()
b.readinto(t2)
print(t2.foo)
print(t2.bar)
1234
b'A'
In [5]:
import struct


struct.pack('<IQ4s', 10, 50, b'Test')
Out[5]:
b'\n\x00\x00\x002\x00\x00\x00\x00\x00\x00\x00Test'
In [6]:
struct.unpack('<IQ4s', b'\n\x00\x00\x002\x00\x00\x00\x00\x00\x00\x00Test')
Out[6]:
(10, 50, b'Test')
In [ ]: