By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,932 Members | 1,944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,932 IT Pros & Developers. It's quick & easy.

python socket usage

P: n/a

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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
Ouz Yarmtepe <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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.