Re: write unsigned integer 32 bits to socket

2008-07-28 Thread Alan Franzoni
Michael Torrie was kind enough to say:

 Of course any time you send coherent numbers over the network, I highly
 recommend you use what's called network byte order.  In C, you'd use the
  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
 endianness is different between the hosts.  Standard convention (even in
 the MS word) is to use big-ending across the network.  I'm sure python
 has some convention in the struct module for dealing with this.

Not in the struct module; such functions are available in the socket
module, and should be employed indeed.


-- 
Alan Franzoni [EMAIL PROTECTED]
-
Remove .xyz from my email in order to contact me.
-
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E
--
http://mail.python.org/mailman/listinfo/python-list


Re: write unsigned integer 32 bits to socket

2008-07-28 Thread Scott David Daniels

Alan Franzoni wrote:

Michael Torrie was kind enough to say:


Of course any time you send coherent numbers over the network, I highly
recommend you use what's called network byte order  I'm sure python
has some convention in the struct module for dealing with this.


Not in the struct module; such functions are available in the socket
module, and should be employed indeed.

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
  @  nativenative
  =  native   standard
 little-endian   standard
  big-endian standard
  !network (= big-endian) standard
  and notes @ is the default.
 print struct.pack('lh', 3,4)
 print struct.pack('lh', 3,4)
 print struct.pack('lh', 3,4)
 print struct.pack('!lh', 3,4)

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


Re: write unsigned integer 32 bits to socket

2008-07-28 Thread [EMAIL PROTECTED]
On Mon, Jul 28, 2008 at 5:45 AM, Scott David Daniels
[EMAIL PROTECTED] wrote:
 Alan Franzoni wrote:

 Michael Torrie was kind enough to say:

 Of course any time you send coherent numbers over the network, I highly
 recommend you use what's called network byte order  I'm sure python
 has some convention in the struct module for dealing with this.

 Not in the struct module; such functions are available in the socket
 module, and should be employed indeed.

 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
  @  nativenative
  =  native   standard
 little-endian   standard
  big-endian standard
  !network (= big-endian) standard
  and notes @ is the default.
 print struct.pack('lh', 3,4)
 print struct.pack('lh', 3,4)
 print struct.pack('lh', 3,4)
 print struct.pack('!lh', 3,4)


thanks for clarifying, and just to make sure, i am using '!' format
from the struct package... i had this even in my previous email
what am doing below is fine right?


this is short
 struct.pack('!h',3)
'\x00\x03'

this is integer
 struct.pack('!i',3)
'\x00\x00\x00\x03'

this is long
 struct.pack('!l',3)
'\x00\x00\x00\x03'
--
http://mail.python.org/mailman/listinfo/python-list


Re: write unsigned integer 32 bits to socket

2008-07-28 Thread Alan Franzoni
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
@  nativenative
=  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
:P

Bye!

-- 
Alan Franzoni [EMAIL PROTECTED]
-
Remove .xyz from my email in order to contact me.
-
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E
--
http://mail.python.org/mailman/listinfo/python-list


Re: write unsigned integer 32 bits to socket

2008-07-28 Thread Grant Edwards
On 2008-07-28, Alan Franzoni [EMAIL PROTECTED] 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
@  nativenative
=  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.compoindexter bar bat??
--
http://mail.python.org/mailman/listinfo/python-list


Re: write unsigned integer 32 bits to socket

2008-07-27 Thread Larry Bates

[EMAIL PROTECTED] wrote:

hi
i want to send unsigned 32 bit integer to socket, and looking for
something equivalent to this method...

http://livedocs.adobe.com/flex/2/langref/flash/net/Socket.html#writeUnsignedInt()

is there such method / library available in python?!


this is as far as i have gotten along

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1',3000))


You will need to use struct module to build the 4 byte value and then send it.

Something like (not tested):

import struct
us32bit = struct.pack(I, value)
s.send(us32bit)

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


Re: write unsigned integer 32 bits to socket

2008-07-27 Thread [EMAIL PROTECTED]
On Sun, Jul 27, 2008 at 7:01 PM, Larry Bates [EMAIL PROTECTED] wrote:
 [EMAIL PROTECTED] wrote:
 i want to send unsigned 32 bit integer to socket, and looking for
 something equivalent to this method...
 http://livedocs.adobe.com/flex/2/langref/flash/net/Socket.html#writeUnsignedInt()

 is there such method / library available in python?!
 You will need to use struct module to build the 4 byte value and then send
 it.

 Something like (not tested):

 import struct
 us32bit = struct.pack(I, value)
 s.send(us32bit)

thanks a lot!!!

just to make sure if I want 32 bit or 4 bytes then should I use the
short or integer or long?

this is short
 struct.pack('!h',3)
'\x00\x03'

this is integer
 struct.pack('!i',3)
'\x00\x00\x00\x03'

this is long
 struct.pack('!l',3)
'\x00\x00\x00\x03'
--
http://mail.python.org/mailman/listinfo/python-list


Re: write unsigned integer 32 bits to socket

2008-07-27 Thread Larry Bates

[EMAIL PROTECTED] wrote:

On Sun, Jul 27, 2008 at 7:01 PM, Larry Bates [EMAIL PROTECTED] wrote:

[EMAIL PROTECTED] wrote:

i want to send unsigned 32 bit integer to socket, and looking for
something equivalent to this method...
http://livedocs.adobe.com/flex/2/langref/flash/net/Socket.html#writeUnsignedInt()

is there such method / library available in python?!

You will need to use struct module to build the 4 byte value and then send
it.

Something like (not tested):

import struct
us32bit = struct.pack(I, value)
s.send(us32bit)


thanks a lot!!!

just to make sure if I want 32 bit or 4 bytes then should I use the
short or integer or long?

this is short

struct.pack('!h',3)

'\x00\x03'

this is integer

struct.pack('!i',3)

'\x00\x00\x00\x03'

this is long

struct.pack('!l',3)

'\x00\x00\x00\x03'


Short is 16 bits, Integer is 32 bits, long is 64 bits (as I read and have 
found).

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


Re: write unsigned integer 32 bits to socket

2008-07-27 Thread [EMAIL PROTECTED]
On Sun, Jul 27, 2008 at 7:17 PM, Larry Bates [EMAIL PROTECTED] wrote:
 [EMAIL PROTECTED] wrote:
 On Sun, Jul 27, 2008 at 7:01 PM, Larry Bates [EMAIL PROTECTED]
 wrote:
 [EMAIL PROTECTED] wrote:
 i want to send unsigned 32 bit integer to socket, and looking for
 something equivalent to this method...

 http://livedocs.adobe.com/flex/2/langref/flash/net/Socket.html#writeUnsignedInt()

 is there such method / library available in python?!

 You will need to use struct module to build the 4 byte value and then
 send
 it.

 Something like (not tested):

 import struct
 us32bit = struct.pack(I, value)
 s.send(us32bit)

 thanks a lot!!!

 just to make sure if I want 32 bit or 4 bytes then should I use the
 short or integer or long?

 this is short

 struct.pack('!h',3)

 '\x00\x03'

 this is integer

 struct.pack('!i',3)

 '\x00\x00\x00\x03'

 this is long

 struct.pack('!l',3)

 '\x00\x00\x00\x03'

 Short is 16 bits, Integer is 32 bits, long is 64 bits (as I read and have
 found).
thanks a lot!!! re-read it again!!!

from the struct doc!
Standard size and alignment are as follows: no alignment is required
for any type (so you have to use pad bytes); short is 2 bytes; int and
long are 4 bytes; long long (__int64 on Windows) is 8 bytes; float and
double are 32-bit and 64-bit IEEE floating point numbers,
respectively.
--
http://mail.python.org/mailman/listinfo/python-list


Re: write unsigned integer 32 bits to socket

2008-07-27 Thread Michael Torrie
[EMAIL PROTECTED] wrote:
 thanks a lot!!! re-read it again!!!
 
 from the struct doc!
 Standard size and alignment are as follows: no alignment is required
 for any type (so you have to use pad bytes); short is 2 bytes; int and
 long are 4 bytes; long long (__int64 on Windows) is 8 bytes; float and
 double are 32-bit and 64-bit IEEE floating point numbers,
 respectively.

Of course any time you send coherent numbers over the network, I highly
recommend you use what's called network byte order.  In C, you'd use the
 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
endianness is different between the hosts.  Standard convention (even in
the MS word) is to use big-ending across the network.  I'm sure python
has some convention in the struct module for dealing with this.
--
http://mail.python.org/mailman/listinfo/python-list