I'm creating a server-client communication, and I want to store some information in a dictionary, so I created a Global dictionary
global commandList commandList = {} And when a client is connection to the server I'm trying to store some information in the following way
self.clientname = str( self.client_address ) commandList[self.clientname]['lastcommand'] = GET_SETUP but I'm getting the following error
commandList[self.clientname]['isready'] = False KeyError: "('134.106.74.22', 49194)" UPDATED:
This is a part of the code.
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()
commandList = {}is done at the top-level of your program, you do not need to addglobal commandList.