|
|
|
|
|
|
| Author |
Message |
Jorgen Grahn *nix forums Guru Wannabe
Joined: 25 Feb 2005
Posts: 107
|
Posted: Sat Feb 05, 2005 9:23 am Post subject:
Re: how to send an int over a socket
|
|
|
On 04 Feb 2005 19:24:29 -0800, Paul Rubin <> wrote:
| Quote: | Tom Brown <brown@esteem.com> writes:
....
Why don't you look at the struct module instead. You could also look
the xdr or xmlrpc libraries, which may be closer to what you want.
|
Or look at existing, successful standards like HTTP, SMTP, etc. Most of them
are text-based -- which wastes a small bit of bandwidth and CPU cycles, but
helps debugging and interoperability a lot.
The xdr module which P.R. refers to implements a simple, standard way of
passing binary datatypes over a network, and is available everywhere, for
all languages. Don't know anything about xmlrpc, except that it is
magnitudes more complex and powerful.
/Jorgen
--
// Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn! |
|
| Back to top |
|
 |
Pierre Quentel *nix forums beginner
Joined: 20 Feb 2005
Posts: 36
|
Posted: Sat Feb 05, 2005 3:51 am Post subject:
Re: how to send an int over a socket
|
|
|
Use string formatting :
msglen = '%16s' %len(testmessage)
will return a 16-byte string, beginning with spaces. Send it over your
connection and use int() to get the message length
Pierre |
|
| Back to top |
|
 |
Paul Rubin *nix forums Guru
Joined: 28 Feb 2005
Posts: 1197
|
Posted: Sat Feb 05, 2005 2:24 am Post subject:
Re: how to send an int over a socket
|
|
|
Tom Brown <brown@esteem.com> writes:
| Quote: | Ok, I think I've found what I was looking for. The marshal.dumps()
function will convert my integer into a string representation of a
fixed size. This way, on the other side, I know how many bytes to
read to get the size of the string.
|
Think hard about whether that's really what you want. marshal.dumps
is not guaranteed to result in a fixed sized string. It's not even
guaranteed to use the same format between (say) Python 2.4 and Python
2.5. So if the two ends of your network connection aren't running the
same version of Python, they might get differing numbers. Marshal
really isn't what you want for that.
Why don't you look at the struct module instead. You could also look
the xdr or xmlrpc libraries, which may be closer to what you want. |
|
| Back to top |
|
 |
Tom Brown *nix forums addict
Joined: 21 Feb 2005
Posts: 57
|
Posted: Sat Feb 05, 2005 1:54 am Post subject:
Re: how to send an int over a socket
|
|
|
On Friday 04 February 2005 18:27, Tom Brown wrote:
| Quote: | Hi,
I have what seems to be a simple problem. But I can not for the life of me
find a way to send an integer over a socket. The send method will only
accept strings. Here is what I am trying to do:
testmessage = 'test message'
msglen = len(testmessage)
sock.send(msglen)
sock.send(testmessage)
Now, I know I can do this:
testmessage = 'test message'
sock.send('\xC') # send hard coded length of testmessage in hex in string
sock.send(testmessage)
However, in my actual program I will not know the length of testmessage in
advance. So how do I convert msglen into a suitable format for the send
method?
Thanks,
Tom
|
Ok, I think I've found what I was looking for. The marshal.dumps() function
will convert my integer into a string representation of a fixed size. This
way, on the other side, I know how many bytes to read to get the size of the
string.
Thanks,
Tom |
|
| Back to top |
|
 |
Paul Rubin *nix forums Guru
Joined: 28 Feb 2005
Posts: 1197
|
Posted: Sat Feb 05, 2005 1:30 am Post subject:
Re: how to send an int over a socket
|
|
|
Tom Brown <brown@esteem.com> writes:
| Quote: | However, in my actual program I will not know the length of testmessage in
advance. So how do I convert msglen into a suitable format for the send
method?
str(123)
'123' |
You might also look at
http://cr.yp.to/proto/netstrings.txt
which describes some issues you should pay attention to, and suggests
a standardized format. |
|
| Back to top |
|
 |
Tom Brown *nix forums addict
Joined: 21 Feb 2005
Posts: 57
|
Posted: Sat Feb 05, 2005 1:27 am Post subject:
how to send an int over a socket
|
|
|
Hi,
I have what seems to be a simple problem. But I can not for the life of me
find a way to send an integer over a socket. The send method will only accept
strings. Here is what I am trying to do:
testmessage = 'test message'
msglen = len(testmessage)
sock.send(msglen)
sock.send(testmessage)
Now, I know I can do this:
testmessage = 'test message'
sock.send('\xC') # send hard coded length of testmessage in hex in string
sock.send(testmessage)
However, in my actual program I will not know the length of testmessage in
advance. So how do I convert msglen into a suitable format for the send
method?
Thanks,
Tom |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Thu Jan 08, 2009 9:55 pm | All times are GMT
|
|
MPAA | Loans | Mortgage insurance | Cheap Magazines | Debt Consolidation
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|