write unsigned integer 32 bits to socket

Grant Edwards grante at visi.com
Mon Jul 28 13:18:25 EDT 2008


On 2008-07-28, Alan Franzoni <alan.franzoni.blahblah at example.com.invalid> wrote:
> Scott David Daniels was kind enough to say:
>
>> Alan Franzoni wrote:
>
>> Please don't pass this misinformation along.
>> 
>> In the struct module document, see the section on the initial character:
>>      Character Byte order Size and alignment
>>        @          native            native
>>        =          native           standard
>>        <       little-endian       standard
>>        >        big-endian         standard
>>        !    network (= big-endian) standard
>
> Sure, that's is one way to do it... but I was answering
> Micheal Torrie, who said:
>
>> htonl() call, and then when pulling it off the wire on the
>> other end you'd use ntohl().  If you don't then you will have
>> problems when the
>
> htonl() and ntohl() are available in Python in the socket
> module, so:
> 1) i was just pointing the OP to the right place where to find
>    such functions
> 2) they work just the same way, hence I can't see why the
>    "struct" way should be the preferred one while the "socket"
>    way should be misinformation

Yes, the socket module does have ntohX and htonX calls.  But
they're superfluous, since you still have to call
struct.pack/unpack to convert integer objects to/from the
byte-strings that are transferred via send() and recv() calls. 

Changing the initial "=" in the format string to a "!"
eliminates the need to pass the integer objects though calls to
socket.ntohX() and socket.htonX()

-- 
Grant Edwards                   grante             Yow! Do you have exactly
                                  at               what I want in a plaid
                               visi.com            poindexter bar bat??



More information about the Python-list mailing list