[Tutor] revisiting struct module

Luke Paireepinart rabidpoobear at gmail.com
Fri Sep 29 01:30:04 CEST 2006


shawn bright wrote:
> Hey there,
> I am writing this because there is something that I am not 
> understanding about the struct module.
Okay, let's see what we can do.
> I have to send a message over a socket. The message has to be exactly 
> 4 bytes, and the last bit has to be the value of 200.
Okay, that makes sense.
Except you used the term 'last bit'.  Do I understand you to mean 'the 
last part' and not 'the last bit (0/1)'?
> so like this:
> null,null,null,200 would be the message, and the 200 has to be an 
> unsigned long int.
Hmm, an unsigned long int?  Isn't that 4 bytes?
Do you  mean
astr = chr(0)+chr(0)+chr(0)+chr(200)
?
I can't really answer your specific question without this information.
>
> I know the stream of info that I am supposed to send the server is big 
> endian.
> I also know that I should be using the struct.pack(), but I don't know 
> how to set this up.
Basically, if the first 3 elements of the messages are null _characters_
and the 200 is an unsigned long int (which is more than a 4-byte string)
formatstr = '>cccL'
packedstruct = struct.pack(formatstr,chr(0),chr(0),chr(0),200)

Is probably how you'd do it.
>
> This is actually only part of the message, but I am trying to learn 
> this as i go.
Sounds like a plan.
HTH,
-Luke


More information about the Tutor mailing list