473,597 Members | 2,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4621
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.c om/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
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
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.c om/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*****@island training.comwri tes:
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
2574
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 "USAGE: echoserver.py <port>"
3
2588
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 webconnection with httpS I always get something in this Area: urllib2.URLError: <urlopen error unknown url type: https>
0
1429
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 someone with lint-like eyes can tell what's wrong. Using xemacs I had hoped that python mode would do more for syntax problems, maybe I'm just not using python mode correctly in xemacs??
10
3679
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. Andrew dalke@dalkescientific.com
0
1567
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 ( -1) / 197 closed ( +1) / 404 total ( +0) New / Reopened Patches ______________________
0
334
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 ( -1) / 229 closed ( +2) / 452 total ( +1) New / Reopened Patches ______________________
0
7977
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8276
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8044
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8261
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6705
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3936
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2409
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.