niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » python
how to send an int over a socket
Post new topic   Reply to topic Page 1 of 1 [6 Posts] View previous topic :: View next topic
Author Message
Jorgen Grahn
*nix forums Guru Wannabe


Joined: 25 Feb 2005
Posts: 107

PostPosted: Sat Feb 05, 2005 9:23 am    Post subject: Re: how to send an int over a socket Reply with quote

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

PostPosted: Sat Feb 05, 2005 3:51 am    Post subject: Re: how to send an int over a socket Reply with quote

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

PostPosted: Sat Feb 05, 2005 2:24 am    Post subject: Re: how to send an int over a socket Reply with quote

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

PostPosted: Sat Feb 05, 2005 1:54 am    Post subject: Re: how to send an int over a socket Reply with quote

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

PostPosted: Sat Feb 05, 2005 1:30 am    Post subject: Re: how to send an int over a socket Reply with quote

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

PostPosted: Sat Feb 05, 2005 1:27 am    Post subject: how to send an int over a socket Reply with 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
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [6 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 9:55 pm | All times are GMT
navigation Forum index » Programming » python
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts socket read modifies third parameter after the call: Very... jainarunk@gmail.com networking 1 Thu Jul 20, 2006 3:47 pm
No new posts Socket Sabotage Michael B Allen networking 0 Wed Jul 19, 2006 9:51 pm
No new posts What blocks a write to a socket pair? Joe networking 0 Wed Jul 19, 2006 3:08 pm
No new posts Socket packet Marco C 10 Wed Jul 19, 2006 8:22 am
No new posts Send mail with attached file rahan Solaris 8 Tue Jul 18, 2006 8:24 pm

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
[ Time: 0.1908s ][ Queries: 20 (0.1052s) ][ GZIP on - Debug on ]