# Buggy code
def get_external_ip_from_known_nodes(
    known_nodes: FleetSensor, sample_size: int = 3, log: Logger = IP_DETECTION_LOGGER
) -> Union[str, None]:
    """
    Randomly select a sample of peers to determine the external IP address
    of this host. The first node to reply successfully will be used.
    # TODO: Parallelize the requests and compare results.
    """
    ip = None
    sample = random.sample(known_nodes, sample_size)
    for node in sample:
        ip = __request(url=node.rest_url())
        if ip:
            log.info(
                f"Fetched external IP address ({ip}) from randomly selected known node(s)."
            )
            return ip


# Traceback (ConnectionRefusedError)
Verbose mode is enabled
Authenticating Ursula
Qualifying worker
✓ Worker is bonded to 0x559A0408aAf3beAb55E80Bd5E2ea83D233f43F7d
✓ Worker is funded with 0.378943746597798709 ETH
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/nucypher/characters/lawful.py", line 1466, in __attempt
teacher = cls.from_seed_and_stake_info(seed_uri=teacher_uri,
File "/usr/lib/python3.8/site-packages/nucypher/characters/lawful.py", line 1507, in from_seed_and_stake_info
certificate = network_middleware.get_certificate(host=host, port=port)
File "/usr/lib/python3.8/site-packages/nucypher/network/middleware.py", line 176, in get_certificate
seednode_certificate = ssl.get_server_certificate(addr=(host, port))
File "/usr/lib/python3.8/ssl.py", line 1483, in get_server_certificate
with  create_connection(addr) as sock:
File "/usr/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/usr/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/bin/nucypher", line 8, in <module>
sys.exit(nucypher_cli())
File "/usr/lib/python3.8/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/usr/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/usr/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/lib/python3.8/site-packages/nucypher/cli/options.py", line 169, in wrapper
return func(**kwargs)
File "/usr/lib/python3.8/site-packages/nucypher/cli/options.py", line 169, in wrapper
return func(**kwargs)
File "/usr/lib/python3.8/site-packages/nucypher/cli/options.py", line 169, in wrapper
return func(**kwargs)
File "/usr/lib/python3.8/site-packages/nucypher/cli/commands/ursula.py", line 390, in run
perform_startup_ip_check(emitter=emitter, ursula=URSULA, force=force)
File "/usr/lib/python3.8/site-packages/nucypher/cli/actions/configure.py", line 157, in perform_startup_ip_check
external_ip = determine_external_ip_address(network=ursula.domain, known_nodes=ursula.known_nodes)
File "/usr/lib/python3.8/site-packages/nucypher/utilities/networking.py", line 172, in determine_external_ip_address
rest_host = get_external_ip_from_default_teacher(network=network)
File "/usr/lib/python3.8/site-packages/nucypher/utilities/networking.py", line 105, in get_external_ip_from_default_teacher
teacher = Ursula.from_teacher_uri(teacher_uri=top_teacher_url,
File "/usr/lib/python3.8/site-packages/nucypher/characters/lawful.py", line 1481, in from_teacher_uri
return __attempt()
File "/usr/lib/python3.8/site-packages/nucypher/characters/lawful.py", line 1477, in __attempt
return __attempt(attempt=attempt + 1)
File "/usr/lib/python3.8/site-packages/nucypher/characters/lawful.py", line 1463, in __attempt
raise ConnectionRefusedError("Host {} Refused Connection".format(teacher_uri))
ConnectionRefusedError: Host https://mainnet.nucypher.network:9151 Refused Connection

# Fixed code
def get_external_ip_from_known_nodes(
    known_nodes: FleetSensor, sample_size: int = 3, log: Logger = IP_DETECTION_LOGGER
) -> Union[str, None]:
    """
    Randomly select a sample of peers to determine the external IP address
    of this host. The first node to reply successfully will be used.
    # TODO: Parallelize the requests and compare results.
    """
    if len(known_nodes) < sample_size:
        return  # There are too few known nodes
    sample = random.sample(list(known_nodes), sample_size)
    client = NucypherMiddlewareClient()
    for node in sample:
        ip = _request_from_node(teacher=node, client=client)
        if ip:
            log.info(
                f"Fetched external IP address ({ip}) from randomly selected known nodes."
            )
            return ip