You should replace those empty excepts with "except Exception:" though. Otherwise looks good.ernitron wrote:I actually use:
This is because you can timeout on any socket operation.Code: Select all
socket = socket.socket() socket.settimeout(0.5) # otherwise it will wait forever socket.listen(1) # maximum number of queued connections while True: try: conn, addr = self.socket.accept() except KeyboardInterrupt: return except: # Timeout do_something_with_timeout_exception() continue try: req = conn.readline() except: # Timeout conn.close() return
EDITED there exist except socket.timeout
How can I trap socket errors?
Re: How can I trap socket errors?
Re: How can I trap socket errors?
It is far better to catch the thing you want to catch (socket.timeout and maybe other OSErrors) and leave other exceptions alone rather than the way you have written it for the simple reason is if you call this from something else in a loop you have now stopped the KeyboardInterrupt so the outer loop will continue and annoy.
If you really want to do it this way, which admittedly might be easier than working out what you're trying to catch raise rather than return from the KeyboardInterrupt handler.
Jon
If you really want to do it this way, which admittedly might be easier than working out what you're trying to catch raise rather than return from the KeyboardInterrupt handler.
Jon
Re: How can I trap socket errors?
Thanks for the added info. I will try working with the added suggestions to be more specific in the error trap.
That may be a while, lately, as I've been adding some 'nice to haves' to my app as I do more thorough testing/beating, I have been running into memory errors as it attempts to load my main.py. So I have to be careful about adding code. I'm trying to learn more about this, I assume the problem is ESP8266 is running out of RAM as it tries to prep the code for running (I'm not even sure if this is truly called a compiling stage, or pre-compiling, or something else - I have some learning to do). But if I have further questions on that, or have anything to share that might help other noobs like myself, I will start a separate thread.
But I also think the added statements aren't going to be critical in this particular case. This app will be running with no keyboard/terminal connection, the socket connection is only made once per hour, a few times per day, for maybe 10-30 seconds to upload the data files, and only after I verified wi-fi is up, then the socket is closed, and control is returned to my main loop which is monitoring changes in a GPIO pin input state. And if there is no added data, that routine isn't even called. In all my testing so far, the only error I had there was when the server socket wasn't set up, and that is likely to happen in real life (server just not running/rebooting, etc), so I definitely needed to trap that error and return control to the main loop.
That may be a while, lately, as I've been adding some 'nice to haves' to my app as I do more thorough testing/beating, I have been running into memory errors as it attempts to load my main.py. So I have to be careful about adding code. I'm trying to learn more about this, I assume the problem is ESP8266 is running out of RAM as it tries to prep the code for running (I'm not even sure if this is truly called a compiling stage, or pre-compiling, or something else - I have some learning to do). But if I have further questions on that, or have anything to share that might help other noobs like myself, I will start a separate thread.
But I also think the added statements aren't going to be critical in this particular case. This app will be running with no keyboard/terminal connection, the socket connection is only made once per hour, a few times per day, for maybe 10-30 seconds to upload the data files, and only after I verified wi-fi is up, then the socket is closed, and control is returned to my main loop which is monitoring changes in a GPIO pin input state. And if there is no added data, that routine isn't even called. In all my testing so far, the only error I had there was when the server socket wasn't set up, and that is likely to happen in real life (server just not running/rebooting, etc), so I definitely needed to trap that error and return control to the main loop.
-
- Posts: 52
- Joined: Thu Apr 20, 2017 9:19 pm
Re: How can I trap socket errors?
Thanks all for the great help on this. Helped me avoid having my nodeMCU basically lock up on a monitoring application I have it doing and updating to influx with urequests.post().