How can I trap socket errors?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: How can I trap socket errors?

Post by deshipu » Wed Nov 23, 2016 9:27 pm

ernitron wrote:I actually use:

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
This is because you can timeout on any socket operation.

EDITED there exist except socket.timeout
You should replace those empty excepts with "except Exception:" though. Otherwise looks good.

jms
Posts: 108
Joined: Thu May 05, 2016 8:29 pm
Contact:

Re: How can I trap socket errors?

Post by jms » Thu Nov 24, 2016 11:50 am

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

NTL2009
Posts: 20
Joined: Wed Oct 26, 2016 10:07 pm

Re: How can I trap socket errors?

Post by NTL2009 » Thu Nov 24, 2016 1:55 pm

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.

eradicatore
Posts: 52
Joined: Thu Apr 20, 2017 9:19 pm

Re: How can I trap socket errors?

Post by eradicatore » Wed May 03, 2017 3:32 am

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().

Post Reply