def open(cls, *addresses, auth, user_agent=None, bolt_versions=None, timeout=0): """ Open a connection to a Bolt server. It is here that we create a low-level socket connection and carry out version negotiation. Following this (and assuming success) a Connection instance will be returned. This Connection takes ownership of the underlying socket and is subsequently responsible for managing its lifecycle. Args: addresses: Tuples of host and port, such as ("127.0.0.1", 7687). auth: user_agent: bolt_versions: timeout: Returns: A connection to the Bolt server. Raises: ProtocolError: if the protocol version could not be negotiated. """ addresses = AddressList(addresses or cls.default_address_list) addresses.resolve() t0 = perf_counter() bolt_versions = cls.fix_bolt_versions(bolt_versions) log.debug("Trying to open connection to «%s»", addresses) errors = set() again = True wait = 0.1 while again: for address in addresses: try: cx = cls._open_to(address, auth, user_agent, bolt_versions) except OSError as e: errors.add(" ".join(map(str, e.args))) else: if cx: return cx again = perf_counter() - t0 < (timeout or 0) if again: sleep(wait) wait *= 2 log.error("Could not open connection to «%s» (%r)", addresses, errors) raise OSError("Could not open connection")
def test_ipv4_only_resolution(): a = AddressList([("localhost", "http")]) a.resolve(family=AF_INET) assert a == [('127.0.0.1', 80)]