当从socket到TKinter绘制数据时,来自pickle的EOFError

时间:2014-04-25 09:33:17

标签: python sockets python-2.7 pickle

对于高级设计,我有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 个答案:

答案 0 :(得分:2)

如果您没有将整个字符串传递给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