Byte oriented data types in python
Grant Edwards
grante at visi.com
Sun Jan 25 18:05:31 EST 2009
On 2009-01-25, Martin v. Löwis <martin at v.loewis.de> wrote:
>> 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?
I'm confused. Are you asking for an introductory tutorial on
programming in Python?
> Right: ON-THE-WIRE, not IN MEMORY. In memory, there is a
> pointer. On the wire, there are no pointers.
I don't understand your point.
> py> CONNECT_REQUEST=17
> py> payload="call me"
> py> encode(CONNECT_REQUEST, len(payload), payload)
> '\x11\x07call me'
If all your data is comprised of 8-bit bytes, then you don't
need the struct module.
--
Grant
More information about the Python-list
mailing list