mirror of
https://github.com/YunoHost/check-http.git
synced 2024-09-03 19:56:42 +02:00
[enh] migrate check_port_is_open to asyncio
This commit is contained in:
parent
4787f328ef
commit
b9ce0ab1c4
1 changed files with 15 additions and 6 deletions
|
@ -289,14 +289,23 @@ async def check_ports(request):
|
||||||
async def check_port_is_open(ip, port):
|
async def check_port_is_open(ip, port):
|
||||||
|
|
||||||
if ":" in ip:
|
if ":" in ip:
|
||||||
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
|
futur = asyncio.open_connection(ip, port, family=socket.AF_INET6)
|
||||||
else:
|
else:
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
futur = asyncio.open_connection(ip, port, family=socket.AF_INET)
|
||||||
sock.settimeout(2)
|
|
||||||
result = sock.connect_ex((ip, port))
|
|
||||||
sock.close()
|
|
||||||
return result == 0
|
|
||||||
|
|
||||||
|
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 #
|
# SMTP check #
|
||||||
|
|
Loading…
Add table
Reference in a new issue