从套接字到TKinter绘图时遇到pickle的EOFError
在我的高级设计项目中,我用LabVIEW把编码器的位置发送给另一个Python脚本,这个脚本会用这些位置来进行正向运动学计算,并实现触觉边界。我需要把这些数据做成一个图形用户界面(GUI)。但是我遇到了一个错误。有没有什么想法可以帮我解决?
-GUIserver.py
# Echo server program
import socket
import pickle
import Tkinter
HOST = 'localhost' # Symbolic name meaning all available interfaces
PORT = 5000 # Arbitrary non-privileged port
addr = (HOST,PORT)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
data = conn.recv(1024)
print(data)
conn.sendall("Welcome Haptic Client: Please send bounrary array via encoded pickle")
data = conn.recv(1024)
boundaryarray = pickle.loads(data)
print repr(boundaryarray)
conn.sendall("GUI loading")
root = Tkinter.Tk()
root.title("Haptic GUI")
w = Tkinter.Canvas(root, width=640, height=480)
w.pack()
#For loops to write all boundary data
for x,y in boundaryarray:
print x
previousx = x
print y
previousy = y
firstx = x
firsty = y
del boundaryarray[0]
break
for x,y in boundaryarray:
print x
print y
print str(previousx) + "x previous"
print str(previousy) + " y previous"
w.create_line(previousx, previousy, x, y, fill="red")
previousx = x
previousy = y
w.create_line(boundaryarray[-1], boundaryarray[-1], firstx, firsty, fill="red")
conn.sendall("The boundary has been plotted, read to take pen data. Send me the current point and the point before that only if the pen array is longer then 2 element")
conn.sendall("To shut down the GUI and socket, send GUI SERVER OFF")
while True:
data = conn.recv(1024)
if data == "GUI SERVER OFF":
break
penarray = pickle.loads(str(data))
for x,y in boundaryarray:
firstx = x
firsty = y
del boundaryarray[0]
break
for x,y in boundaryarray:
secondx = x
secondy = y
w.create_line(firstx, firsty, secondy, secondx, fill="red")
w.canvas.draw()
conn.sendall("plotted")
root.mainloop()
-hapticclient.py
import socket
import pickle
import random
HOST = 'localhost' # The remote host
PORT = 5000 # The same port as used by the server
addr = (HOST,PORT)
array = [(6.0, 6.0000000674792586), (6.8888888888888893, 8.514157444218835), (7.7777777777777777, 9.3259176771323915), (8.6666666666666661, 9.7712361663282543), (9.5555555555555554, 9.9752319599996255), (10.444444444444445, 9.9752319599996255), (11.333333333333332, 9.7712361663282543), (12.222222222222221, 9.3259176771323933), (13.111111111111111, 8.5141574442188368), (14.0, 6.0000000674792586), (6.0, 5.9999999325207414), (6.8888888888888893, 3.4858425557811645), (7.7777777777777777, 2.6740823228676081), (8.6666666666666661, 2.2287638336717466), (9.5555555555555554, 2.0247680400003749), (10.444444444444445, 2.0247680400003749), (11.333333333333332, 2.2287638336717466), (12.222222222222221, 2.6740823228676072), (13.111111111111111, 3.4858425557811636), (14.0, 5.9999999325207414)]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('Hello, GUI. This is haptic')
data = s.recv(1024)
if data == "Welcome Haptic Client: Please send bounrary array via encoded pickle":
#We serialize the data with picle here
s.sendall(pickle.dumps(array))
penarray = []
data = s.recv(1024)
if data == "The boundary has been plotted, read to take pen data. Send me the current point and the point before that only if the pen array is longer then 2 element":
while True:
pen_array_to_send = []
location = (random.randint(10, 100), random.randint(10, 100))
print location
penarray.append(location)
if len(penarray) > 2:
pen_array_to_send.append(penarray[-1])
pen_array_to_send.append(penarray[-2])
#s.sendall(pickle.dumps(pen_array_to_send))
print pen_array_to_send
data = s.recv(1024)
print 'Received', repr(data)
s.close()
print 'Received', repr(data)
1 个回答
2
当你没有把整个字符串传给pickle,而只是传了一部分时,就会出现EOFError错误。这种情况可能发生在你通过conn.recv(1024)
从套接字读取不完整的字符串时。
我会把
data = conn.recv(1024)
boundaryarray = pickle.loads(data)
改成
f = conn.makefile("r")
boundaryarray = pickle.load(f)
因为conn.recv(1024)
会接收0到1024个字节。我会把它改成
f = conn.makefile("r")
boundaryarray = pickle.load(f)
这样可以确保读取到pickle所需要的所有内容,既不多也不少。
如果你要保存一些东西,记得使用
f = conn.makefile("w")
boundaryarray = pickle.dump(...)
和文件一起。
我还建议你看看这个链接:Python实现简单的网页数据存储