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

R

Ryan Grow

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
 
T

Tim Keating

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
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top