EOFError: ran out of input. Getting this error when trying to pickle.loads from a socket
EOFError: ran out of input. Getting this error when trying to pickle.loads from a socket
我有一个 numpy ndarray
正试图通过套接字连接发送。当我尝试在服务器上加载它时,使用 pickle.loads
我得到 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()
我试过阅读 this, this and this 但没有任何帮助。
我正在使用 Python 3.4.
编辑:
准确的错误是:
File server.py, line 30, in <module>:
M = pickle.loads(data) #HERE IS AN ERROR
EOFError: Ran out of input.
conn.recv(length)
不一定读取 length
字节,如果可用的字节数少于该数目。你需要循环直到你有足够的。
见When does socket.recv(recv_size) return?
data = b''
l = length
while l > 0:
d = sock.recv(l)
l -= len(d)
data += d
在您使用 .send()
的代码中存在类似的问题,尽管在这种情况下有一个简单的解决方法:只需像在其他地方那样使用 .sendall()
。
我有一个 numpy ndarray
正试图通过套接字连接发送。当我尝试在服务器上加载它时,使用 pickle.loads
我得到 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()
我试过阅读 this, this and this 但没有任何帮助。
我正在使用 Python 3.4.
编辑:
准确的错误是:
File server.py, line 30, in <module>:
M = pickle.loads(data) #HERE IS AN ERROR
EOFError: Ran out of input.
conn.recv(length)
不一定读取 length
字节,如果可用的字节数少于该数目。你需要循环直到你有足够的。
见When does socket.recv(recv_size) return?
data = b''
l = length
while l > 0:
d = sock.recv(l)
l -= len(d)
data += d
在您使用 .send()
的代码中存在类似的问题,尽管在这种情况下有一个简单的解决方法:只需像在其他地方那样使用 .sendall()
。