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))
rkmr.em@gmail.com 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
On Sun, Jul 27, 2008 at 7:01 PM, Larry Bates <larry.bates@websafe.com`> wrote: > rkmr.em@gmail.com 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'
rkmr.em@gmail.com wrote: > On Sun, Jul 27, 2008 at 7:01 PM, Larry Bates <larry.bates@websafe.com`> wrote: >> rkmr.em@gmail.com 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
On Sun, Jul 27, 2008 at 7:17 PM, Larry Bates <larry.bates@websafe.com`> wrote: > rkmr.em@gmail.com wrote: >> On Sun, Jul 27, 2008 at 7:01 PM, Larry Bates <larry.bates@websafe.com`> >> wrote: >>> rkmr.em@gmail.com 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.
rkmr.em@gmail.com 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.
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 <alan.franzoni.xyz@gmail.com> - Remove .xyz from my email in order to contact me. - GPG Key Fingerprint: 5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E
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 @ native native = 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)
On Mon, Jul 28, 2008 at 5:45 AM, Scott David Daniels <Scott.Daniels@acm.org> 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 > @ native native > = 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'
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 :P Bye! -- Alan Franzoni <alan.franzoni.xyz@gmail.com> - Remove .xyz from my email in order to contact me. - GPG Key Fingerprint: 5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E
On 2008-07-28, Alan Franzoni <alan.franzoni.blahblah@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??