blob: e82875d9f526e2221b2ae6d8e40bc0e5eff390ee (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# -----------------------------------------------------------------------------
# System Imports
# -----------------------------------------------------------------------------
from typing import Optional
import socket
import asyncio
# -----------------------------------------------------------------------------
# Public Imports
# -----------------------------------------------------------------------------
from httpx import URL
# -----------------------------------------------------------------------------
# Exports
# -----------------------------------------------------------------------------
__all__ = ["port_check_url"]
# -----------------------------------------------------------------------------
#
# CODE BEGINS
#
# -----------------------------------------------------------------------------
async def port_check_url(url: URL, timeout: Optional[int] = 5) -> bool:
"""
This function attempts to open the port designated by the URL given the
timeout in seconds. If the port is avaialble then return True; False
otherwise.
Parameters
----------
url:
The URL that provides the target system
timeout: optional, default is 5 seonds
Time to await for the port to open in seconds
"""
port = url.port or socket.getservbyname(url.scheme)
try:
wr: asyncio.StreamWriter
_, wr = await asyncio.wait_for(
asyncio.open_connection(host=url.host, port=port), timeout=timeout
)
# MUST close if opened!
wr.close()
return True
except Exception: # noqa
return False
|