Byte oriented data types in python
"Martin v. Löwis"
martin at v.loewis.de
Sun Jan 25 17:28:55 EST 2009
> dtype = ord(rawdata[0])
> dcount = struct.unpack("!H",rawdata[1:3])
> if dtype == 1:
> fmtstr = "!" + "H"*dcount
> elif dtype == 2:
> fmtstr = "!" + "f"*dcount
> rlen = struct.calcsize(fmtstr)
>
> data = struct.unpack(fmtstr,rawdata[3:3+rlen])
>
> leftover = rawdata[3+rlen:]
Unfortunately, that does not work in the example. We have
a message type (an integer), and a variable-length string.
So how do you compute the struct format for that?
>> Sure. You would normally have a struct such as
>>
>> struct TLV{
>> char type;
>> char length;
>> char *data;
>> };
>>
>> However, the in-memory representation of that struct is *not*
>> meant to be sent over the wire. In particular, the character
>> pointer has no meaning outside the address space, and is thus
>> not to be sent.
>
> Well if it's not representing the layout of the data we're
> trying to deal with, then it's irrelevent. We are talking
> about how convert python objects to/from data in the
> 'on-the-wire' format, right?
Right: ON-THE-WIRE, not IN MEMORY. In memory, there is a
pointer. On the wire, there are no pointers.
> Like this?
>
>>>> def encode(type,length,value):
> ... return chr(type)+chr(length)+value
> ...
>>>> print encode('float', 1, 3.14159)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<stdin>", line 2, in encode
> TypeError: an integer is required
No:
py> CONNECT_REQUEST=17
py> payload="call me"
py> encode(CONNECT_REQUEST, len(payload), payload)
'\x11\x07call me'
Regards,
Martin
More information about the Python-list
mailing list