472,126 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 software developers and data experts.

python socket usage


Is it possible to send a data object like a tuple or a list in socket
programming? If so how? It seems with socket module it is only possible to
send strings.
--
Oğuz Yarımtepe
http://www.yarimtepe.com/en
Aug 16 '07 #1
6 4553
On 16 Sie, 09:42, O uz Yar mtepe <comp....@gmail.comwrote:
Is it possible to send a data object like a tuple or a list in socket
programming? If so how? It seems with socket module it is only possible to
send strings.

--
O uz Yar mtepehttp://www.yarimtepe.com/en
Hi Oguz,

why don't you make a string out of your tuple, or list, send it via
socket and make a tuple/list again?
>>x = (1,2,3,4,5,6,7,)
type(x)
<type 'tuple'>
>>y = str(x)
type(y)
<type 'str'>
>>print y
(1, 2, 3, 4, 5, 6, 7)
>>z = tuple(y)
type(z)
<type 'tuple'>
>>>
Cheers,
Marek

Aug 16 '07 #2
O¿uz YarÑmtepe <co******@gmail.comwrites:
Is it possible to send a data object like a tuple or a list in socket
programming? If so how? It seems with socket module it is only possible to
send strings.
Look into something like pyro or JSON. Whatever you do, don't use eval.
Aug 16 '07 #3
markacy wrote:
On 16 Sie, 09:42, O uz Yar mtepe <comp....@gmail.comwrote:
>Is it possible to send a data object like a tuple or a list in socket
programming? If so how? It seems with socket module it is only possible to
send strings.

--
O uz Yar mtepehttp://www.yarimtepe.com/en

Hi Oguz,

why don't you make a string out of your tuple, or list, send it via
socket and make a tuple/list again?

>>>x = (1,2,3,4,5,6,7,)
type(x)
<type 'tuple'>
>>>y = str(x)
type(y)
<type 'str'>
>>>print y
(1, 2, 3, 4, 5, 6, 7)
>>>z = tuple(y)
type(z)
<type 'tuple'>
Sure it's a tuple, but did you look at it? Turning a sequence (which is
what a string is after all) into a tuple is an easy operation, but it's
surely not what you want here.
>>x = (1,2,3,4,5,6,7)
s = str(x)
t = tuple(s)
t
('(', '1', ',', ' ', '2', ',', ' ', '3', ',', ' ', '4', ',', ' ', '5',
',', ' ', '6', ',', ' ', '7', ')')

If you want to turn a string representation of an object back into an
object, you must eval the string.
Moreover, if the tuple contains things other than simple integers (float
of strings of whatever) the string represtnataion of the object may not
be able to recover the original object accurately. Worse yet, an eval
of an arbitrary string is a HUGE security hole.

If you really want to send any Python object through a socket, look up
the Pickle and cPickle modules. These will marshal (as it's called) any
Python object of any type and complexity into a byte string which can be
sent across a socket. On the receiving end of the socket, the byte
string can be turned back into an equivalent Python object.

Gary Herron
>
Cheers,
Marek

Aug 16 '07 #4
Gary Herron <gh*****@islandtraining.comwrites:
be able to recover the original object accurately. Worse yet, an eval
of an arbitrary string is a HUGE security hole.

If you really want to send any Python object through a socket, look up
the Pickle and cPickle modules.
These are also security holes.
Aug 16 '07 #5
On Thursday 16 August 2007 11:20:38 Gary Herron wrote:
If you really want to send any Python object through a socket, look up
the Pickle and cPickle modules. Â*These will marshal (as it's called)any
Python object of any type and complexity into a byte string which can be
sent across a socket. Â*On the receiving end of the socket, the byte
string can be turned back into an equivalent Python object.

Gary Herron
As i read pickle module is Python-spesific. I need to talk with a Java
application and get the infortion that it will send. What i do right now is
listening a socket and reding the string that is sent by the java
application. So the java application is sending a string and i am reading and
parsing it and getting the related infortion i need. A more professional way
may be the reading the object itself. Is it possible to get the array for ex.
object that is sent from the Java application with sockets?

--
Oğuz Yarımtepe
http://www.yarimtepe.com/en
Aug 16 '07 #6
Oğuz Yarımtepe wrote:
As i read pickle module is Python-spesific. I need to talk with a Java
application and get the infortion that it will send. What i do right now is
listening a socket and reding the string that is sent by the java
application. So the java application is sending a string and i am reading and
parsing it and getting the related infortion i need. A more professional way
may be the reading the object itself. Is it possible to get the array for ex.
object that is sent from the Java application with sockets?
No. Sockets send and receive byte. Any transfer of higher-level
object values requires the sender to encode the values into bytes,
and the receiver to parse the bytes to construct an object; even
then the result is a value copy, not the object itself.

For many kinds of objects, there are libraries available to do
the encoding and parsing. If you need reference semantics, there
are "object request brokers".

Say more about your problem, and there's a good chance you'll
get more useful answers.
--
--Bryan
Aug 17 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by xunil | last post: by
reply views Thread by richard.hubbell | last post: by
10 posts views Thread by Andrew Dalke | last post: by
reply views Thread by Kurt B. Kaiser | last post: by
reply views Thread by Kurt B. Kaiser | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.