pxblat.check_port_in_use(host: str, port: int, tries: int = 3) bool[source]

Check if a given port on a host is in use.

Parameters:
host : str

The hostname or IP address of the host to check.

port : int, optional

The port number to check. Defaults to DEFAULT_PORT.

tries : int, optional

The number of times to attempt the check. Defaults to 3.

Returns:

True if the port is in use, False otherwise.

Return type:

bool

Raises:
  • TypeError – If the host argument is not a string.

  • ValueError – If the port argument is not a valid port number.

This function attempts to connect to the specified host and port using the socket library. It will attempt the connection a number of times specified by the tries argument. If the connection is successful, it will return True, indicating that the port is in use. If the connection fails, it will return False.

If the host argument is not a string, a TypeError will be raised. If the port argument is not a valid port number, a ValueError will be raised.

Example

>>> check_port_in_use("localhost", 8080)
True