parsing packets

geremy condra debatem1 at gmail.com
Sun Jul 10 17:34:10 EDT 2011


On Sun, Jul 10, 2011 at 4:59 PM, Littlefield, Tyler <tyler at tysdomain.com> wrote:
> Hello all:
> I'm working on a server that will need to parse packets sent from a client,
> and construct it's own packets.
> The setup is something like this: the first two bytes is the type of the
> packet.
> So, lets say we have a packet set to connect. There are two types of connect
> packet: a auth packet and a connect packet.
> The connect packet then has two bytes with the type, another byte that notes
> that it is a connect packet, and 4 bytes that contains the version of the
> client.
> The auth packet has the two bytes that tells what packet it is, one byte
> denoting that it is an auth packet, then the username, a NULL character, and
> a password and a NULL character.
>
> With all of this said, I'm kind of curious how I would 1) parse out
> something like this (I am using twisted, so it'll just be passed to my
> Receive function), and how I get the length of the packet with multiple NULL
> values. I'm also looking to build a packet and send it back out, is there
> something that will allow me to designate two bytes, set individual bits,
> then put it altogether in a packet to be sent out?

Just use the struct module[0] to do your packing for you.  There's not
enough info in your description to write it out for you, but it sounds
like it should do everything you need.

Geremy Condra

[0]: http://docs.python.org/library/struct.html



More information about the Python-list mailing list