cast
is returning the internal pointer of the Python string. Don't pass an immutable Python string to a function that expects a mutable buffer. Use create_string_buffer
to create a mutable char
array (read section 16.17.1.4. Fundamental data types). For the offset, use byref
with the optional offset
argument:
send_buf = ctypes.create_string_buffer(b'\0' * 18 + payload + b'\0' * 4)
lws.libwebsocket_write(wsi, ctypes.byref(send_buf, 18), len(payload), 0)
send_buf.value # null terminated
send_buf.raw # complete array