Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python equivalent of java ObjectOutputStream and ObjectInputStream?

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:

  • python equivalent of java OutputStream?
like image 652
user Avatar asked Mar 27 '26 19:03

user


2 Answers

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.

like image 81
Rajesh J Advani Avatar answered Mar 30 '26 07:03

Rajesh J Advani


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)
like image 25
clampfferj Avatar answered Mar 30 '26 08:03

clampfferj