I have a numpy ndarray
that I'm trying to send via socket connection. When I try to load it on the server, using pickle.loads
I get EOFError: ran out of input
.
client.py
import numpy as np
import socket, pickle
import struct
HOST = "192.168.143.xxx"
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
centers = np.zeros((100, 43919))
packet = pickle.dumps(centers)
length = struct.pack('>I', len(packet))
packet = length + packet
s.send(packet)
data = s.recv(8192)
data_new = pickle.loads(data)
s.close()
print ('Received', data_new)
server.py
import socket, pickle, numpy as np
import struct
HOST = ''
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(2)
while 1:
L = np.zeros((100, 43919))
#wait to accept a connection - blocking call
conn, addr = s.accept()
print ('Connected with ', addr)
buf = b''
while len(buf) < 4:
buf += conn.recv(4 - len(buf))
length = struct.unpack('>I', buf)[0]
print(length)
data = conn.recv(length)
if not data: break
M = pickle.loads(data) # HERE IS AN ERROR, the same as np.loads(...)
L += M
data_out = pickle.dumps(L)
conn.sendall(data_out)
conn.close()
s.close()
I've tried reading this, this and this but nothing helps.
I'm using Python 3.4.
EDIT:
The exact error is:
File server.py, line 30, in <module>:
M = pickle.loads(data) #HERE IS AN ERROR
EOFError: Ran out of input.