check_host¶
-
privex.helpers.net.
check_host
(host: Union[str, ipaddress.IPv4Address, ipaddress.IPv6Address], port: Union[decimal.Decimal, int, float, str], version='any', throw=False, **kwargs) → bool[source]¶ Test if the service on port
port
for hosthost
is working. AsyncIO version:check_host_async()
Basic usage (services which send the client data immediately after connecting):
>>> check_host('hiveseed-se.privex.io', 2001) True >>> check_host('hiveseed-se.privex.io', 9991) False
For some services, such as HTTP - it’s necessary to transmit some data to the host before it will send a response. Using the
send
kwarg, you can transmit an arbitrary string/bytes upon connection.Sending data to
host
after connecting:>>> check_host('files.privex.io', 80, send=b"GET / HTTP/1.1\n\n") True
- Parameters
host (str|IPv4Address|IPv6Address) – Hostname or IP to test
port (int|str) – Port number on
host
to connect toversion (str|int) – When connecting to a hostname, this can be set to
'v4'
,'v6'
or similar to ensure the connection is via that IP versionthrow (bool) – (default:
False
) WhenTrue
, will raise exceptions instead of returningFalse
kwargs – Additional configuration options (see below)
receive (int) – (default:
100
) Amount of bytes to attempt to receive from the server (0
to disable)send (bytes|str) – If
send
is specified, the data insend
will be transmitted to the server before receiving.stype (int) – Socket type, e.g.
socket.SOCK_STREAM
timeout (float|int) – Socket timeout. If not passed, uses the default from
socket.getdefaulttimeout()
. If the global default timeout isNone
, then falls back to5.0
- Raises
socket.timeout – When
throw=True
and a timeout occurs.socket.gaierror – When
throw=True
and various errors occurConnectionRefusedError – When
throw=True
and the connection was refusedConnectionResetError – When
throw=True
and the connection was reset
- Return bool success
True
if successfully connected + sent/received data. OtherwiseFalse
.