473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 4608
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: xunil | last post by:
hi, could any one please help show me how to run a python script in linux ? when i try "python script.py" it just does nothing this is the script iam trying to run #!/usr/bin/env python...
3
by: Phillip | last post by:
Some people tipped me off on some possibilities to tackle my https problem. Those have definitely gotten me further in cornering the problem. Thank you. But: No matter what I do to open a...
0
by: richard.hubbell | last post by:
I am sure this is old news, the syntax of python is crazy to me. There I said it, I'm sure I'll get over it or around it. I was trying to make a whois script work and was unable. May be...
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 391 open ( +7) / 3028 closed (+12) / 3419 total (+19) Bugs : 906 open ( -3) / 5519 closed (+19) / 6425 total (+16) RFE : 207 open...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 393 open (+15) / 3315 closed (+17) / 3708 total (+32) Bugs : 908 open (+22) / 5975 closed (+49) / 6883 total (+71) RFE : 223 open...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.