Designing socket messaging format

Donn Cave donn at drizzle.com
Sun Nov 11 23:45:19 EST 2001


Quoth shriek at gmx.co.uk (Stephen):
| I'm developing a program which sends messages back and forth 
| between two servers using sockets.  The message format I've 
| been given is "YYYYTTTTTTTTTTTTTTTTTTTTTTT"
|
| Where ~
| Y is the message type, which in turn affects the message length.
| T represents a text message. (eg. "Hello World"). Note that 
|   it could also contain newlines (eg. "Hello World\nGood bye")
...
| Other than facilitating use of SocketServer.ThreadingTCPServer,
| it seems that asyncore also looks out for termination strings,
| and the "Python Sockets HowTo" infers that using fixed-length
| for message termination is a bad idea.  So everything seems to
| be pointing this way.

I just looked briefly through asyncore.py, and I don't see the
termination string support you mention.  As I understood it, the
readline() in your SocketServer example was your own code, and
your decision to use a line format.  I didn't look at the howto.

If you want to use a line separated format, that's OK with me,
and \r\n is definitely a classic line separator.  But it isn't
by any means the only right way to do things, and particularly
if the data can conceivably under any circumstances contain the
separator, it's actually the wrong way.  The right way would,
of course, be XML!  (Just kidding.)  A common alternative to
line separation and fixed length is counted length - send the
size of the text data, and then the text itself.

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list