[Twisted-Python] How to write binary data from a protocol?

Heiko Wundram heikowu at ceosg.de
Sat May 1 03:56:09 EDT 2004


Am Samstag, 1. Mai 2004 04:34 schrieb Carl Waldbieser:
> I tried calling self.transport.write(), but I received
> an error indicating that only string data is accepted.

Have a look at the struct module. To pack an integer (actually an unsigned 
long) as a 4 byte string, you would do:

self.transport.write(struct.pack("!L",5))

On the receiving side, you would read 4 bytes, and unpack them using:

struct.unpack("!L",data)[0]

But all the rest is best explained using the docs of the struct module...

Heiko.




More information about the Twisted-Python mailing list