how do I read a series of pickled objects from a socket?

Discussion in 'Python' started by Ryan Grow, Nov 16, 2004.

  1. Ryan Grow

    Ryan Grow Guest

    Hi,

    I am trying to figure out the correct and most
    reliable way to read a series of pickled objects from
    a socket.
    I've found examples that do something like this:

    while 1:
    data = conn.recv(500)
    obj = pickle.loads(data)
    # do stuff with object

    I don't think the above example would work for my
    application. The above example might work if the
    server never expects to get more than one object on
    the socket at a time.

    The server I'm writing will get a series of objects so
    it will need to know where each pickled object ends in
    the stream so it knows where to start reading the next
    object from.

    Is there a correct way to identify the terminator of a
    pickled object? Is there a standard and correct way
    for the pickle module to easily read pickled objects
    from a socket?


    Thanks,

    Ryan



    __________________________________
    Do you Yahoo!?
    Meet the all-new My Yahoo! - Try it today!
    http://my.yahoo.com
    Ryan Grow, Nov 16, 2004
    #1
    1. Advertising

  2. Ryan Grow

    Tim Keating Guest

    I would strongly caution you against doing this, unless you are
    *positive* you can secure that endpoint at the network level.
    Promiscuously accepting arbitrary python objects to execute from an
    open network connection is a recipe for disaster IMHO.

    OTOH, if you are convinced this is how you want to do this . . . when
    you send the pickle, send a fixed size length field (set to the size of
    the pickle) first. So the pseudocode would look something like:

    size = conn.recv(4)
    data = conn.recv(size)
    obj = pickle.loads(data)

    Also, you probably want to have a look at the select module, unless you
    never want your server to shut down :)

    Tim Keating
    Tim Keating, Nov 16, 2004
    #2
    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. Bram Stolk
    Replies:
    0
    Views:
    249
    Bram Stolk
    Dec 12, 2005
  2. krishnakant Mane
    Replies:
    2
    Views:
    219
    Bruno Desthuilliers
    May 4, 2007
  3. krishnakant Mane
    Replies:
    2
    Views:
    263
    Daniele Varrazzo
    May 7, 2007
  4. Jean-Paul Calderone

    Re: Pickled objects over the network

    Jean-Paul Calderone, Jul 18, 2007, in forum: Python
    Replies:
    3
    Views:
    714
    Irmen de Jong
    Jul 19, 2007
  5. milan_sanremo
    Replies:
    3
    Views:
    271
    Lawrence Oluyede
    Oct 19, 2007
Loading...

Share This Page