help on packet format for tcp/ip programming

Gabriel Genellina gagsl-py at yahoo.com.ar
Wed Feb 7 22:26:46 EST 2007


En Thu, 08 Feb 2007 00:14:13 -0300, <rattan at cps.cmich.edu> escribió:

> On Feb 8, 1:43 am, Bjoern Schliessmann <usenet-
> mail-0306.20.chr0n... at spamgourmet.com> wrote:
>> rat... at cps.cmich.edu wrote:
>> > I want a specific packet format for packet exchange between a
>> > client server across the network. For example frame format
>> > as a python class could be:
>> > class Frame:
>> >         def __init__(self, buffer=None, count=0, offset=0):
>> >                 self.buffer = buffer
>> >                 self.count = count
>> >                 self.offset = offset
>> > the question is how to convert it to a byte stream so that format
>> > of count and offset also becomes a sequence of bytes.
>>
>> Try struct.
>>
>
> struct module pack and unpack will only work for fixed size buffer :
> pack('>1024sIL', buffer, count. offset) but the buffer size can vary
> from one packet to the next  :-(

Use struct to pack count and offset into a fixed size field (2 or 4 bytes  
depending on range); send buffer after that.
buffer must come *after* you send its size, else it's a lot harder to  
retrieve at the other end.

-- 
Gabriel Genellina




More information about the Python-list mailing list