Sending Dictionary via Network

M

mumebuhi

Hi,

Is it possible to send a non-string object from a Python program to
another? I particularly need to send a dictionary over to the other
program. However, this is not possible using the socket object's send()
function.

Help?


Buhi
 
L

Larry Bates

mumebuhi said:
Hi,

Is it possible to send a non-string object from a Python program to
another? I particularly need to send a dictionary over to the other
program. However, this is not possible using the socket object's send()
function.

Help?


Buhi
You will need to pickle it first and unpickle it on the other
end. Other than that, you should be able to send it just fine.

-Larry
 
M

mumebuhi

Thank you very much for the reply.

Can pickle work directly with socket? The way I am doing right now is
to pickle the object to a file then send the file content through the
socket.
 
B

Bruno Desthuilliers

mumebuhi a écrit :
Hi,

Is it possible to send a non-string object from a Python program to
another? I particularly need to send a dictionary over to the other
program. However, this is not possible using the socket object's send()
function.

Help?
True

http://cheeseshop.python.org/pypi/simplejson

If you *really* want to "share" objects between programs, there are way
to do so (pyro comes to mind). But this gets more complicated...
 
M

mumebuhi

The simplejson module is really cool and simple to use. This is great!

For others who are just starting to use Python (like myself), simply
download and decompress the source bundle then copy the simplejson
directory to your sys.path (e.g. /usr/lib/python2.4/).

Thanks so much, Bruno.


Buhi
 
B

Ben Finney

You want what is called "serialisation": turning a data structure in
Python into a predictable sequence of (usually text) bytes, to turn it
back into an identical data structure at some other point in time.

said:
You will need to pickle it first and unpickle it on the other end.

There are many serialisation schemes possible; 'pickle' is just one
(and may be the right one in this case).

Others include JSON, marshal, some XML schema, etc.
 
S

Simon Forman

mumebuhi said:
Thank you very much for the reply.

Can pickle work directly with socket? The way I am doing right now is
to pickle the object to a file then send the file content through the
socket.

Pickle aso has dumps() and loads() to work with strings rather than
files.

Peace,
~Simon
 
S

Steve Holden

mumebuhi said:
The simplejson module is really cool and simple to use. This is great!

For others who are just starting to use Python (like myself), simply
download and decompress the source bundle then copy the simplejson
directory to your sys.path (e.g. /usr/lib/python2.4/).
Alternatively, if you want a complete installation,

EITHER go to

http://cheeseshop.python.org/pypi/simplejson

download and extract the source then run

python setup.py install

in the extracted directory.

OR if you have easy_install, just run

easy_install simplejson

If you don't have easy_install, get it!
Thanks so much, Bruno.
regards
Steve
 
F

Frithiof Andreas Jensen

The simplejson module is really cool and simple to use. This is great!

JUST what I need for some configuration files!!
Thanks for the link (die, configparse, dieee).
 
L

Leif K-Brooks

Frithiof said:
JUST what I need for some configuration files!!
Thanks for the link (die, configparse, dieee).


I would personally use YAML for configuration files instead of JSON,
because it's more human-readable. But it's a matter of personal preference.
 
C

Chris Mellon

Pickle aso has dumps() and loads() to work with strings rather than
files.


I would recommend against using pickle if you're going to push it over
a network - there are signifigant dangers in unpickling anything
untrusted.
 
F

Frithiof Andreas Jensen

Leif K-Brooks said:
I would personally use YAML for configuration files instead of JSON,
because it's more human-readable. But it's a matter of personal preference.

Hehe: I am using YAML now. I find it readable - human ... hmm ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top