列表索引超出范围错误

时间:2012-09-28 04:05:01

标签: python list

当我运行此代码时,继续使索引超出范围错误我如何解决这个问题?数据包含一些值,这些值将通过服务器传递给valueList,我想在数据中使用这些值的一部分。例如,int(valueList [8])包含球的x坐标。我该如何解决这个问题?

import os,sys
import pygame
import socket
import time
from multiprocessing import Process, Value
from ctypes import c_bool

Host = '59.191.193.45'
Port = 5555

def updatePosition(valueList, update_valueList, quit_flag):
    while not quit_flag.value:
        print 'Ball x', int(valueList[8])
        print 'Ball y', int(valueList[9])
        update_valueList = valueList[:]
    print 'Closing child update process'

def activateRobot(update_valueList, quit_flag): 
    while not quit_flag.value:
        print 'Ball x', int(valueList[8])
        print 'Ball y', int(valueList[9])
        print 'turn to'
        print 'move to', int(valueList[8])
        print 'move to', int(valueList[9])
    print 'Closing child activate robot process'

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((Host,Port))

valueList = []
update_valueList = []
quit_flag = Value(c_bool,False)

update = Process(target = updatePosition, args=(valueList, update_valueList,quit_flag))
activate = Process(target = activateRobot, args=(update_valueList,quit_flag))

update.start()
activate.start()

while True:
    client.sendall("loc\n")
    data = client.recv(8192)
    if not data:
        print 'network connection close by client'
        break
    valueList = data.split()

print 'All done, closing child process'
update.join()
activate,join()

1 个答案:

答案 0 :(得分:3)

data = client.recv(8192)

接收最多 8192个字节。你可能会得到部分线条。为此,该行不必长于8192字节。你只需要阅读到目前为止通过网络到达的内容,这可能不是整行。