Stop Python script from shutting down on small errors
I have a Python socket server. In this socket server, when a new client
connects and successfully authenticates themselves, it'll grab their json
encoded information from the database. When it decodes it, it goes to a
variable. Sometimes in the object, a key doesn't exist. I'll get a
KeyError and the whole script will shut down. For example:
A client sends a packet to the server The server handles it like this:
def handleReceiveThisPacket(self, data):
myInfo = self.info['exampleKey']
return self.send(myInfo)
If "exampleKey" doesn't exist in the user's info (hey, it happens, like if
I added a new key to the info for a new packet to handle something else
and the user's info hasn't been updated yet), the whole script shuts down.
I know I could easily do try, except but I often call keys throughout the
script and I think it would look messy having a bunch of try and excepts
everywhere, don't you think? Is there an easier way to just make it so
Python gives off a warning about it but doesn't shut down, similar to how
PHP does it?
Also, the client is a seperate class.
No comments:
Post a Comment