how to send an int over a socket

Paul Rubin http
Fri Feb 4 22:24:29 EST 2005


Tom Brown <brown at esteem.com> writes:
> Ok, I think I've found what I was looking for. The marshal.dumps()
> function will convert my integer into a string representation of a
> fixed size. This way, on the other side, I know how many bytes to
> read to get the size of the string.

Think hard about whether that's really what you want.  marshal.dumps
is not guaranteed to result in a fixed sized string.  It's not even
guaranteed to use the same format between (say) Python 2.4 and Python
2.5.  So if the two ends of your network connection aren't running the
same version of Python, they might get differing numbers.  Marshal
really isn't what you want for that.

Why don't you look at the struct module instead.  You could also look
the xdr or xmlrpc libraries, which may be closer to what you want.



More information about the Python-list mailing list