I need to send numpy arrays between Python2 and Python3 programs which run simultaneously. After doing some research, I've decided to send pickles over a socket connection.
I'm able to establish a connection and send strings back and forth, but trying to send numpy arrays results in a ascii error. Here is what my client/server are doing:
### Imports on both client and server ###
import socket as sc
import numpy
try:
import cPickle as pickle
except ImportError:
import pickle
pickle.HIGHEST_PROTOCOL = 2
### Python2 client ###
data= numpy.ones((1, 60))
sock.connect(SERVER_ADDR)
try:
serialized_data = pickle.dumps(data, protocol=2)
except pickle.PicklingError as e:
raise e
try:
sock.send(serialized_data )
except sc.error as e:
logging.error('Connection ended')
### Python3 server ###
"""
Given a client socket, and until the client has closed the session:
- receive input from client
- unpickle and print it
"""
while client != '':
try:
data = client.recv(4096)
except sc.error as e:
if e.errno != errno.ECONNRESET:
print('Connection ended')
else:
try:
unserialized_input = pickle.loads(input) ##### ERROR HERE #####
except pickle.UnpicklingError as e:
raise e
else:
print(unserialized_input)
### Error on server ###
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf0 in position 6: ordinal not in range(128)
I trying searching for related questions, but couldn't find anything relevant except the solution suggested here, which doesn't work for me since I need to use Python2. I'm not sure how to continue. Any ideas?