Sending Dictionary via Network

Discussion in 'Python' started by mumebuhi, Oct 24, 2006.

  1. mumebuhi

    mumebuhi Guest

    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
     
    mumebuhi, Oct 24, 2006
    #1
    1. Advertising

  2. mumebuhi

    Larry Bates Guest

    mumebuhi wrote:
    > 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
     
    Larry Bates, Oct 24, 2006
    #2
    1. Advertising

  3. mumebuhi

    mumebuhi Guest

    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.
     
    mumebuhi, Oct 25, 2006
    #3
  4. 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?



    >>> d = dict(one=1, two="three", question="life, universe, and everything")
    >>> import simplejson
    >>> s = simplejson.dumps(d)
    >>> s

    '{"question": "life, universe, and everything", "two": "three", "one": 1}'
    >>> simplejson.loads(s) == d

    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...
     
    Bruno Desthuilliers, Oct 25, 2006
    #4
  5. mumebuhi

    mumebuhi Guest

    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


    Bruno Desthuilliers wrote:
    > 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?

    >
    >
    > >>> d = dict(one=1, two="three", question="life, universe, and everything")
    > >>> import simplejson
    > >>> s = simplejson.dumps(d)
    > >>> s

    > '{"question": "life, universe, and everything", "two": "three", "one": 1}'
    > >>> simplejson.loads(s) == d

    > 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...
     
    mumebuhi, Oct 25, 2006
    #5
  6. mumebuhi

    Ben Finney Guest

    Larry Bates <> writes:

    > mumebuhi wrote:
    > > 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.


    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.

    <URL:http://en.wikipedia.org/wiki/Serialization>

    > 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.

    --
    \ "A child of five could understand this. Fetch me a child of |
    `\ five." -- Groucho Marx |
    _o__) |
    Ben Finney
     
    Ben Finney, Oct 25, 2006
    #6
  7. mumebuhi

    Simon Forman Guest

    mumebuhi wrote:
    > 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
     
    Simon Forman, Oct 25, 2006
    #7
  8. mumebuhi

    Steve Holden Guest

    mumebuhi wrote:
    > 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
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
     
    Steve Holden, Oct 25, 2006
    #8

  9. > "mumebuhi" <> wrote in message

    news:...
    > 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).
     
    Frithiof Andreas Jensen, Oct 25, 2006
    #9
  10. Frithiof Andreas Jensen wrote:
    >> "mumebuhi" <> wrote in message

    > news:...
    >> 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).



    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.
     
    Leif K-Brooks, Oct 25, 2006
    #10
  11. mumebuhi

    Chris Mellon Guest

    On 24 Oct 2006 16:56:43 -0700, Simon Forman <> wrote:
    >
    > mumebuhi wrote:
    > > 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.



    I would recommend against using pickle if you're going to push it over
    a network - there are signifigant dangers in unpickling anything
    untrusted.
     
    Chris Mellon, Oct 25, 2006
    #11
  12. Lawrence Oluyede, Oct 25, 2006
    #12
  13. "Leif K-Brooks" <> wrote in message
    news:453f5b4b$0$13814$...
    > Frithiof Andreas Jensen wrote:
    > >> "mumebuhi" <> wrote in message

    > > news:...
    > >> 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).

    >
    >
    > 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 ;-)
     
    Frithiof Andreas Jensen, Oct 26, 2006
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    6
    Views:
    483
    Ilias Lazaridis
    Feb 21, 2006
  2. james_027
    Replies:
    1
    Views:
    362
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  3. Navkirat Singh
    Replies:
    6
    Views:
    3,331
    Navkirat Singh
    Jul 29, 2010
  4. Chris Rebert
    Replies:
    0
    Views:
    573
    Chris Rebert
    Jul 29, 2010
  5. Fox

    dictionary within dictionary

    Fox, Mar 8, 2005, in forum: ASP General
    Replies:
    5
    Views:
    213
    Michael D. Kersey
    Mar 13, 2005
Loading...

Share This Page