Packet parsing problem...
ben.hutchings at roundpoint.com
Tue Feb 27 22:37:08 CET 2001
Erno Kuusela <erno-news at erno.iki.fi> writes:
> In article <97ek6i$o6d at news.or.intel.com>, "Brian Geddes"
> <brian.j.geddes at intel.com> writes:
> | Here's my situation:
> | There is an already-existing server (written in C++), which
> | communicates with clients through 1-packet messages. Each packet
> | consists of a header of 2 or 3 C++ DWORDs, followed by 0 to n bytes
> | of data. I have to write a Python client to communicate with this
> | server.
> | In python, is there any structure that approximates the C++ DWORD?
> | I need to be able to parse incoming packets, as well as properly
> | form messages to the server.
> it would be nice to have a struct-ish module in the standard
> distribution that would be geared towards working with data
> formats with known bit layout...
We already have one! You can use a modifier at the beginning of a
struct format string to select one of 5 size, byte ordering and
alignment rules (the first one being the default, native, rules).
So I suppose Brian might read messages using something like this:
from struct import *
(type,) = unpack(message[0:4], '<L')
if type == TYPE_EGGS:
param1, length = unpack(message[4:8], '<LL')
param2 = message[8:length]
elif type == TYPE_SPAM:
I'm not sure quite how he'd want to deal with invalid messages; the
above doesn't attempt to do anything useful with them since he hasn't
said much about the message format.
Any opinions expressed are my own and not necessarily those of Roundpoint.
More information about the Python-list