Close Socket

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
MorseIot
Posts: 38
Joined: Fri Jun 29, 2018 12:32 am

Close Socket

Post by MorseIot » Thu Apr 25, 2019 9:27 pm

According to the sample code below, sending packet to a host on a given port, if socket error occurs, depending on the code, the socket will be closed and returns None. But when resending, always returns socket error, something wrong ???

Code: Select all

def openSocket(host,port,send,buffer):
        try:
	    _socket = socket.socket()
            _socketAddr = socket.getaddrinfo(host, port)[0][-1]
            _socket.connect(_socketAddr)
            if port is 443:
                _socket = ssl.wrap_socket(_socket)
            _socket.settimeout(timeout)
            _socket.setblocking(False)
            _socket.send(send)
            responseBytes = _socket.recv(buffer)
        except:
            print('Socket Except Error')
            responseBytes = None
            pass
        finally:
            close()
            print('Socket Closed Finally')
        return responseBytes

def close()
    _socket.close() 

#Exit only recv is not None
responseBytes = None
While responseBytes is not None:
   openSocket(xxxxx,80,'xxxxxxx',512)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Close Socket

Post by pythoncoder » Fri Apr 26, 2019 5:41 am

You have made your code hard to debug by trapping all exceptions and issuing a generic message. I would suggest at the very least getting it to issue something more meaningful so you can figure out why it's failing

Code: Select all

        except Exception as e:
            print('Socket Except Error', e)
            responseBytes = None
#            pass  This line has no purpose as far as I can see
Or, for test purposes,

Code: Select all

        except:
            #print('Socket Except Error')
            raise
            responseBytes = None
 
Then you'll get a full traceback with the failing line number.
Peter Hinch
Index to my micropython libraries.

User avatar
MorseIot
Posts: 38
Joined: Fri Jun 29, 2018 12:32 am

Re: Close Socket

Post by MorseIot » Fri Apr 26, 2019 9:41 pm

pythoncoder wrote:
Fri Apr 26, 2019 5:41 am
You have made your code hard to debug by trapping all exceptions and issuing a generic message. I would suggest at the very least getting it to issue something more meaningful so you can figure out why it's failing

Code: Select all

        except Exception as e:
            print('Socket Except Error', e)
            responseBytes = None
#            pass  This line has no purpose as far as I can see
Or, for test purposes,

Code: Select all

        except:
            #print('Socket Except Error')
            raise
            responseBytes = None
 
Then you'll get a full traceback with the failing line number.
When using raise in except, the error happens after some time OSError: [Errno 103] ECONNABORTED relative to the line of code _socket.connect (_socketAddr).

I have not found a solution so far for this error

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Close Socket

Post by pythoncoder » Sat Apr 27, 2019 6:00 am

I think this means that the other endpoint is rejecting the connection.

I notice you're using a nonblocking socket. This raises at least two issues. As I understand it, SSL/TLS on nonblocking sockets is a very recent addition to MicroPython. It may or may not have issues.

Secondly you can't just send and receive from a nonblocking socket as the methods guarantee to return immediately regardless of how much of the data was sent or received. There may well be a shortfall. You have to monitor this, repeating any partially complete operations until all the data has been handled. There are numerous examples on the web. There is MicroPython code in this file - see the _as_read and _as_write methods.
Peter Hinch
Index to my micropython libraries.

User avatar
MorseIot
Posts: 38
Joined: Fri Jun 29, 2018 12:32 am

Re: Close Socket

Post by MorseIot » Sat Apr 27, 2019 12:09 pm

pythoncoder wrote:
Sat Apr 27, 2019 6:00 am
I think this means that the other endpoint is rejecting the connection.

I notice you're using a nonblocking socket. This raises at least two issues. As I understand it, SSL/TLS on nonblocking sockets is a very recent addition to MicroPython. It may or may not have issues.

Secondly you can't just send and receive from a nonblocking socket as the methods guarantee to return immediately regardless of how much of the data was sent or received. There may well be a shortfall. You have to monitor this, repeating any partially complete operations until all the data has been handled. There are numerous examples on the web. There is MicroPython code in this file - see the _as_read and _as_write methods.
Okay, I'll read and test this solution.
Thanks

Post Reply