[enh] migrate check_port_is_open to asyncio

This commit is contained in:
Laurent Peuch 2021-01-08 03:05:28 +01:00
parent 4787f328ef
commit b9ce0ab1c4

View file

@ -289,14 +289,23 @@ async def check_ports(request):
async def check_port_is_open(ip, port):
if ":" in ip:
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
futur = asyncio.open_connection(ip, port, family=socket.AF_INET6)
else:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(2)
result = sock.connect_ex((ip, port))
sock.close()
return result == 0
futur = asyncio.open_connection(ip, port, family=socket.AF_INET)
try:
_, writer = await asyncio.wait_for(futur, timeout=2)
except (asyncio.TimeoutError, ConnectionRefusedError):
return False
except Exception:
import traceback
traceback.print_exc()
return False
else:
writer.close()
await writer.wait_closed()
return True
# ########################################################################### #
# SMTP check #