def _get_free_port(host: str) -> int:
    """
    Gets a free port by opening a socket, binding it, checking the assigned
    port, and then closing it.
    """
    s = socket.socket()
    s.bind((host, 0))
    port = s.getsockname()[1]
    s.close()
    return port  # type: ignore[no-any-return]

    Gets a free port by opening a socket, binding it, checking the assigned
    port, and then closing it.
    
test/contrib/test_socks.py