Guaranteeing an n-byte data type?

Evan Jones chardish at gmail.com
Fri Jan 9 11:14:16 EST 2009


Hello all,

I'm trying to use sockets to implement a pre-defined network protocol that
requires that I send messages of exactly a certain number of bytes. In
Python, integer values are represented as 4 bytes each (AFAIK.) However I
don't want to always send 4 bytes: sometimes I want to send one byte, or 11
bytes, or 33 bytes, or any other permutation that's not a multiple of 4. It
seems to make the most sense to use one-byte data members and concatenate
them before sending.

This is reasonably easy in C (thanks to the uint8_t data type), but with
Python I'm not sure how I'd implement it. The send() method in the socket
module will take any kind of data, but you can't specify the number of bytes
you want to send, so there's no guarantee as to how many you're actually
sending (particularly if you're sending a value that's regarded in Python as
a long integer - who knows how that data is actually represented in memory
behind the scenes!)

Perhaps since I'm trying to perform low-level operations, Python is simply
the wrong tool for this job. However, I'd very much like to write this
implementation in Python for sake of quick implementation and testing.

Cheers,
Evan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090109/bcd4257a/attachment.html>


More information about the Python-list mailing list