python中的字典

2024-09-22 10:18:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个服务器-客户端通信,我想在字典中存储一些信息,所以我创建了一个全局字典

global commandList
commandList = {}

当客户机连接到服务器时,我试图用以下方式存储一些信息

self.clientname = str( self.client_address )
commandList[self.clientname]['lastcommand'] = GET_SETUP

但我有以下错误

commandList[self.clientname]['isready'] = False
KeyError: "('134.106.74.22', 49194)"

更新:

这是代码的一部分。

class MCRequestHandler( SocketServer.BaseRequestHandler ):

    global clientsLock, postbox, rxQueue, disconnect, isRunning, commandList
    postbox = {}    
    rxQueue = Queue.Queue()
    disconnect = {}
    commandList = {}
    clientsLock = threading.RLock()
    isRunning = {}


    def setup( self ):               
        clientsLock.acquire()        
        if len( postbox ) == 0:            
            self.clientname = 'MasterClient'
            postbox['MasterClient'] = Queue.Queue()            
            mess = str( self.client_address );            

            postbox['MasterClient'].put( self.createMessage( MASTER_CLIENT_CONNECTED, mess ) )
            print "Client name: %s" % str( self.clientname )  
            self.request.settimeout( 0.1 )                     
        else:            
            #Send message to the master client            
            self.clientname = str( self.client_address )               
            print "Client name:%s" % self.clientname

            postbox[self.clientname] = Queue.Queue()

            #Setting up the last command
            if not commandList.has_key( self.clientname ):
                commandList[self.clientname] = {}

            commandList[self.clientname]['lastcommand'] = GET_SETUP
            commandList[self.clientname]['isready'] = False            

            self.request.settimeout( COMMANDTABLE[commandList[self.clientname]['lastcommand']]['timeout'] )           

            self.transNr = 0;    
            self.start = True;
            isRunning[self.clientname] = True;

        disconnect[self.clientname] = True                   
        clientsLock.release() 

Tags: selfclienttrue字典queueaddressdisconnectstr