Network byte ordering question ...

ian stevens istevens at calum.csclub.uwaterloo.ca
Mon Jun 5 21:22:14 EDT 2000


I am writing a Python application which makes use of a protocol which
requires information to be in network byte order.  Specifically, a
message header takes on the following form:

    bytes   type

    0-15    Message ID ... all numbers
    16      Numeric value
    17      Numeric value
    18      Numeric value
    19-22   Numeric value

What is the best way to extricate the above information and convert it
to the byte ordering of the host machine?  To convert a header in the
byte ordering of the host machine to network byte ordering?  How do
I determine the type of machine the program is running on and, hence,
its byte ordering?

I've had a look the pack() and unpack() commands in the struct module
but without much success at getting them to do what I want.  I am new
to Python and it's been a while since I performed this type of operation
in C or C++, so I'm a little rusty.

FYI, here's what I've tried:

    ( val1, val2, val3, val4, val5 ) = unpack( "@16s1s1s1s4s", header )

This works somewhat but I would really like val1 to read as a string
containing numeric values (to preserve leading zeroes) and the other
values to read as numbers.  This is also specific to little-endian
machines and I'd like to generalize it.

Any help or references would be greatly appreciated.

thanks,
ian.



More information about the Python-list mailing list