3

In java I can transfer objects between server and client by using Object Output Stream and Object Input Stream. Is there anything equivalent in python?

Related:

Community
  • 1
  • 1
user
  • 5,335
  • 7
  • 47
  • 63
  • Python might prefer to use JSOn or XML instead of having its own binary format. – Peter Lawrey May 22 '12 at 17:01
  • Actually I'm trying to transfer object between two processes in same machines. These objects aren't purely data objects. I'm not sure if it'll work. – user May 22 '12 at 17:04
  • Reminds me of this statement 'XML is not the answer. It is not even the question. To paraphrase Jamie Zawinski on regular expressions, "Some people, when confronted with a problem, think "I know, I'll use XML." Now they have two problems."' http://dirtsimple.org/2004/12/python-is-not-java.html – Rajesh J Advani May 22 '12 at 17:06

2 Answers2

9

The pickle module in Python provides object serialization and deserialization functionality. http://docs.python.org/library/pickle.html

It's not particularly secure, so you should always validate the incoming data, but it should support your needs.

Rajesh J Advani
  • 5,585
  • 2
  • 23
  • 35
3

The multiprocessing module has the Pipe() function that handles serializing and passing objects between processes. http://docs.python.org/library/multiprocessing.html#multiprocessing.Pipe

example (pipes work within the same process too)

import multiprocessing

class ObjectToSend:
    def __init__(self,data):
        self.data = data

obj = ObjectToSend(['some data'])

#create 2 read/write ends to a pipe
a, b = multiprocessing.Pipe()
#serialize and send obj across the pipe using send method
a.send(obj) 
#deserialize object at other end of the pipe using recv method
print(b.recv().data)
clampfferj
  • 31
  • 1