EOFError:用完了输入。尝试从套接字pickle.loads时出现此错误

时间:2015-11-12 14:24:12

标签: python sockets numpy pickle

我有一个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()

我已经尝试过阅读thisthisthis,但没有任何帮助。

我正在使用Python 3.4。

编辑:

确切的错误是:

File server.py, line 30, in <module>:
M = pickle.loads(data) #HERE IS AN ERROR
EOFError: Ran out of input.

1 个答案:

答案 0 :(得分:3)

如果可用的数量少于conn.recv(length),那么

length不一定会读取data = b'' l = length while l > 0: d = sock.recv(l) l -= len(d) data += d 个字节。你需要循环,直到你有足够的。

请参阅When does socket.recv(recv_size) return?

.send()

您的代码中存在使用.sendall()的类似问题,但在这种情况下可以轻松解决:只需像在其他地方一样使用filteredArrayUsingPredicate: