sock.connect(socket.getaddrinfo(ip,port)[0][-1]) takes too long to respond
I'm trying to use usocket library in my application on ESP32 DevkitC and I want to develop a software where if I input a wrong host address it should respond quickly, reset and try again through a webserver based configuration.
sock.connect(socket.getaddrinfo(slave_ip, slave_port)[0][-1])
During testing, I intentionally input the wrong address and it takes around 19 seconds, after which it responds with this error:
[Errno 113] EHOSTUNREACH
I want to reduce this time so that i can perform some other actions.
P.S: sock.setblocking(False) is not an option.
WiPy/CC3200: usocket.socket.settimeout() not working for socket.getaddrinfo()
In issue https://github.com/micropython/micropython/issues/2402 a solution was proposed to make socket.settimeout() working for socket.connect(). It seems to work well.
However it does not work for socket.getaddrinfo() as there seems to be fixed timeout of 19s.
import socket
import ssl
import time
def test():
start = time.time()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SEC)
s = ssl.wrap_socket(s, cert_reqs=ssl.CERT_REQUIRED, ca_certs='/flash/cert/ca.pem')
s.settimeout(3)
try:
print(socket.getaddrinfo("www.google.com", 443)[0][-1])
except Exception as e:
print(e)
finish = time.time()
print(finish - start)
s.close()
EDIT: Looking at the code once again it is obvious that calling settimeout() method of socket instance cannot affect timeout of socket.getaddrinfo(). Anyway is there any way how to modify timeout of this method?