Packet parsing problem...

Brian Quinlan BrianQ at ActiveState.com
Mon Feb 26 17:43:26 EST 2001


A DWORD is just a signed 32-bit integer. Your parsing code could look
something like this (usual disclaimer of little thought and no testing).
Actually, this is pretty ugly (integerize could be a lot cleaner). There has
to be library code to do this...

import socket

# Do communication stuff here e.g. header = mySocket.recv( 2 * 4 )

def integerize( aString ):
	myInt = 0
	for i in range(4):
		myInt = myInt + ord( aString[i] ) << 8 * (3 - i )
	return myInt

word1 = socket.ntohl( integerize( header[0:3] ) )
word2 = socket.ntohl( integerize( header[4:7] ) )
# ... Do more stuff

-----Original Message-----
From: python-list-admin at python.org
[mailto:python-list-admin at python.org]On Behalf Of Brian Geddes
Sent: Monday, February 26, 2001 2:11 PM
To: python-list at python.org
Subject: Packet parsing problem...


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.

Any suggestions?

Thanks,
- Brian


--
http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list