Is it possible to serialize some object using pickle and use it in other independent module in Python3?

Viewed 886

I have 2 independent files: The first one is client.py and in it i should create some object using Record and then to serialize it using pickle class and then to send it with socket to the second file which is server.py and the server should de-serialize this bytes object to the original object

client.py:

from pyrecord import Record
import pickle

clientSock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
clientSock.connect(("localhost",48621))
Message1Obj=Record.create_type('Message1Obj','name','address')
message1Data = Message1Obj("KB","aa")
message1Serlization = pickle.dumps(message1Data)
clientSock.send(message1Serlization)

server.py

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind(("localhost",48621))
sock.listen(5)
con,adr = sock.accept()

message1 = con.recv(8192)
Message1Obj = pickle.loads(message1)

but i get this error on the servers.py:

AttributeError: Can't get attribute 'Message1Obj' on module 'main' from 'server.py'

I understood from previous answers that pickle.dump saves also the module that the object is in, Is it possible to load the serialized object in other module?

1 Answers

Instead of using the pickle to serialize the I have used dill to serialize my python object and send over server in binary string format.

message1Data = {'A message or function/class object'}
 import dill
 s_data = dill.dumps(message1Data)

send to server through some socket and De-serialize it there

org_data = dill.loads(s_data)
Related