Skip to content

network

dev_tool.tools.network

log = logging.getLogger(__name__) module-attribute

derive_port

A function that derives a stable port from a project name.

The port is the project name's SHA-256 digest folded into the window that starts at the base port, so every project lands on a port of its own and lands on the same one on every machine and every run.

Parameters:

  • name (str) –

    The project name the port is derived from.

  • base (int) –

    The first port of the window.

  • span (int) –

    The number of ports in the window.

Returns:

  • int –

    The derived port number.

Source code in dev_tool/tools/network.py
def derive_port(name: str, base: int, span: int) -> int:
    """
    A function that derives a stable port from a project name.

    The port is the project name's SHA-256 digest folded into the window that
    starts at the base port, so every project lands on a port of its own and
    lands on the same one on every machine and every run.

    :param name: The project name the port is derived from.
    :param base: The first port of the window.
    :param span: The number of ports in the window.
    :return: The derived port number.
    """

    if span < 1:
        message = f'Port window must hold at least one port: {span}'
        raise ValueError(message)

    digest = hashlib.sha256(name.encode('utf-8')).hexdigest()
    integer = int(digest, 16)

    return base + (integer % span)

get_local_ip

A function that gets the local IP address of the machine.

On Linux/WSL, returns '0.0.0.0' to allow external access.

Returns:

  • str –

    The local IP address, or '127.0.0.1' if it cannot be determined.

Source code in dev_tool/tools/network.py
def get_local_ip() -> str:
    """
    A function that gets the local IP address of the machine.

    On Linux/WSL, returns '0.0.0.0' to allow external access.

    :return: The local IP address, or '127.0.0.1' if it cannot be determined.
    """

    if sys.platform != OperatingSystem.WINDOWS:
        return '0.0.0.0'  # noqa: S104

    try:
        with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as handle:
            connection = ('8.8.8.8', 80)
            handle.connect(connection)
            ip, _ = handle.getsockname()

            return ip
    except Exception:
        return '127.0.0.1'

is_port_ready

A function that waits for a port to accept connections.

Parameters:

  • host (str) –

    The host address to check.

  • port (int) –

    The port number to check.

  • timeout (float, default: 30.0 ) –

    The maximum time to wait in seconds.

Returns:

  • bool –

    True if the port is ready, False if the timeout was reached.

Source code in dev_tool/tools/network.py
def is_port_ready(host: str, port: int, timeout: float = 30.0) -> bool:
    """
    A function that waits for a port to accept connections.

    :param host: The host address to check.
    :param port: The port number to check.
    :param timeout: The maximum time to wait in seconds.
    :return: True if the port is ready, False if the timeout was reached.
    """

    start = time.monotonic()

    while time.monotonic() - start < timeout:
        try:
            with socket.create_connection((host, port), timeout=1):
                return True
        except (ConnectionRefusedError, OSError):
            time.sleep(0.5)

    return False