Sending Dictionary via Network

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mumebuhi

    #1

    Sending Dictionary via Network

    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

  • Larry Bates

    #2
    Re: Sending Dictionary via Network

    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

    Comment

    • Bruno Desthuilliers

      #3
      Re: Sending Dictionary via Network

      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.dump s(d)
      >>s
      '{"question": "life, universe, and everything", "two": "three", "one": 1}'
      >>simplejson.lo ads(s) == d
      True



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

      Comment

      • mumebuhi

        #4
        Re: Sending Dictionary via Network

        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.

        Comment

        • mumebuhi

          #5
          Re: Sending Dictionary via Network

          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.dump s(d)
          >>s
          '{"question": "life, universe, and everything", "two": "three", "one": 1}'
          >>simplejson.lo ads(s) == d
          True
          >

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

          Comment

          • Ben Finney

            #6
            Re: Sending Dictionary via Network

            Larry Bates <larry.bates@we bsafe.comwrites :
            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.or g/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

            Comment

            • Simon Forman

              #7
              Re: Sending Dictionary via Network


              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

              Comment

              • Steve Holden

                #8
                Re: Sending Dictionary via Network

                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



                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

                Comment

                • Frithiof Andreas Jensen

                  #9
                  Re: Sending Dictionary via Network

                  "mumebuhi" <mumebuhi@gmail .comwrote in message
                  news:1161732155 .264210.77250@f 16g2000cwb.goog legroups.com...
                  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).


                  Comment

                  • Leif K-Brooks

                    #10
                    Re: Sending Dictionary via Network

                    Frithiof Andreas Jensen wrote:
                    >"mumebuhi" <mumebuhi@gmail .comwrote in message
                    news:1161732155 .264210.77250@f 16g2000cwb.goog legroups.com...
                    >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.

                    Comment

                    • Chris Mellon

                      #11
                      Re: Sending Dictionary via Network

                      On 24 Oct 2006 16:56:43 -0700, Simon Forman <rogue_pedro@ya hoo.comwrote:
                      >
                      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.

                      Comment

                      • Lawrence Oluyede

                        #12
                        Re: Sending Dictionary via Network

                        Chris Mellon <arkanes@gmail. comwrote:
                        I would recommend against using pickle if you're going to push it over
                        a network - there are signifigant dangers in unpickling anything
                        untrusted.
                        I confirm: http://jcalderone.livejournal.com/15864.html

                        --
                        Lawrence - http://www.oluyede.org/blog

                        "Nothing is more dangerous than an idea
                        if it's the only one you have" - E. A. Chartier

                        Comment

                        • Frithiof Andreas Jensen

                          #13
                          Re: Sending Dictionary via Network


                          "Leif K-Brooks" <eurleif@ecritt ers.bizwrote in message
                          news:453f5b4b$0 $13814$4d3efbfe @news.sover.net ...
                          Frithiof Andreas Jensen wrote:
                          "mumebuhi" <mumebuhi@gmail .comwrote in message
                          news:1161732155 .264210.77250@f 16g2000cwb.goog legroups.com...
                          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 ;-)


                          Comment

                          Working...