Baversjo Baversjo - 1 year ago 88
Python Question

Implementing a binary protocol in Python (twisted)

I've written a TCP server in node.js implementing a binary protocol.
Using buffers this was very easy in node.js.

I'm looking for a similar way to implement my protocol in python 2.7 using twisted.
However, if there's a better tool for the job combining python 3 and some sort of evented I/O, please let me know. Because of bytes object in python 3, at first look it seams to me that python 3 is better at handling binary protocols. I need to handle many TCP(and UDP) connections and write a bit of GUI.

Here's how implement my protocol in node.js:

var buf = new Buffer(5+data.length), //headers + data (data is also a buffer)
action = 0x01;

buf[0] = action;
buf.writeUInt16(data.length,1,'big'); //write content length in at offset 1
buf.writeUint16(12345,5,'big'); //message identifier at offset 3
data.copy(buf,5); //copy data into message at offset 5


I would appreciate examples for how to achieve similar instructions in python.

Edit: this is how I was able to implement my protocol using the python struct module:

self.transport.write(struct.pack('!bHH',action,data.length, sock_id) + data)

Answer Source

It sounds like the standard Python struct module will do what you need. It allows you to specify exactly what the layout of binary data needs to be.

The capabilities of handling binary protocols in Python 2 and Python 3 are equivalent.