【问题标题】:EOFError from pickle when plotting data from socket to TKinter将数据从套接字绘制到 TKinter 时来自 pickle 的 EOFError
【发布时间】:2014-04-25 09:33:16
【问题描述】:

对于高级设计,我让 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)

【问题讨论】:

    标签: python sockets python-2.7 pickle


    【解决方案1】:

    如果您没有将整个字符串传递给 pickle,而仅传递其中的一部分,则会发生 EOFError。 当您使用conn.recv(1024) 从套接字读取不完整的字符串时,可能会发生这种情况。

    我会改变

    data = conn.recv(1024)
    boundaryarray = pickle.loads(data)
    

    因为conn.recv(1024) 接收 0 到 1024 个字节。我会把它改成

    f = conn.makefile("r")
    boundaryarray = pickle.load(f)
    

    因为它读取了泡菜想要的所有内容,而不是更多而不是更少。

    如果你转储一些东西,使用

    f = conn.makefile("w")
    boundaryarray = pickle.dump(...)
    

    文件。

    我也建议看看这个:Python implementing simple web data storage

    【讨论】:

    • 我现在明白了,谢谢,这有效!您详细解释了问题所在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 2021-06-14
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多