async def _do_connect(self, protocol_factory: IProtocolFactory) -> None: first_exception = None server_list = await self._resolve_server() for server in server_list: host = server.host port = server.port try: logger.debug("Connecting to %s:%i", host.decode("ascii"), port) endpoint = HostnameEndpoint(self._reactor, host, port) if self._tls_options: endpoint = wrapClientTLS(self._tls_options, endpoint) result = await make_deferred_yieldable( endpoint.connect(protocol_factory)) return result except Exception as e: logger.info("Failed to connect to %s:%i: %s", host.decode("ascii"), port, e) if not first_exception: first_exception = e # We return the first failure because that's probably the most interesting. if first_exception: raise first_exception # This shouldn't happen as we should always have at least one host/port # to try and if that doesn't work then we'll have an exception. raise Exception("Failed to resolve server %r" % (self._parsed_uri.netloc, ))
def __try_to_connect_to_address(self, connect_info: TCPConnectInfo): address = connect_info.socket_addresses[0].address port = connect_info.socket_addresses[0].port logger.debug("Connection to host %r: %r", address, port) use_ipv6 = connect_info.socket_addresses[0].ipv6 use_hostname = connect_info.socket_addresses[0].hostname if use_ipv6: endpoint = TCP6ClientEndpoint(self.reactor, address, port, self.timeout) elif use_hostname: endpoint = HostnameEndpoint(self.reactor, address, port, self.timeout) else: endpoint = TCP4ClientEndpoint(self.reactor, address, port, self.timeout) defer = endpoint.connect(self.outgoing_protocol_factory) defer.addCallback(self.__connection_established, self.__connection_to_address_established, connect_info) defer.addErrback(self.__connection_failure, self.__connection_to_address_failure, connect_info)
class LoggingHostnameEndpoint(object): """A wrapper for HostnameEndpint which logs when it connects""" def __init__(self, reactor, host, port, *args, **kwargs): self.host = host self.port = port self.ep = HostnameEndpoint(reactor, host, port, *args, **kwargs) def connect(self, protocol_factory): logger.info("Connecting to %s:%i", self.host.decode("ascii"), self.port) return self.ep.connect(protocol_factory)
class LoggingHostnameEndpoint(object): """A wrapper for HostnameEndpint which logs when it connects""" def __init__(self, reactor, host, port, *args, **kwargs): self.host = host self.port = port self.ep = HostnameEndpoint(reactor, host, port, *args, **kwargs) logger.info("Endpoint created with %s:%d", host, port) def connect(self, protocol_factory): logger.info("Connecting to %s:%i", self.host.decode("ascii"), self.port) return self.ep.connect(protocol_factory)
def _new_connection(self, key: Tuple, uri: URI, endpoint: HostnameEndpoint) -> Deferred: self._pending_requests[key] = deque() conn_lost_deferred = Deferred() conn_lost_deferred.addCallback(self._remove_connection, key) factory = H2ClientFactory(uri, self.settings, conn_lost_deferred) conn_d = endpoint.connect(factory) conn_d.addCallback(self.put_connection, key) d = Deferred() self._pending_requests[key].append(d) return d
def connect(self, host): point = HostnameEndpoint(self._reactor, host, SCPI_PORT) d = point.connect(SCPIClientFactory()) @d.addCallback def connect_vrt(scpi): self._scpi = scpi point = HostnameEndpoint(self._reactor, host, VRT_PORT) return point.connect(VRTClientFactory(self._vrt_callback)) @d.addCallback def save_vrt(vrt): self._vrt = vrt return d
class LoggingHostnameEndpoint: """A wrapper for HostnameEndpint which logs when it connects""" def __init__(self, reactor: IReactorTime, host: bytes, port: int, *args: Any, **kwargs: Any): self.host = host self.port = port self.ep = HostnameEndpoint(reactor, host, port, *args, **kwargs) logger.info("Endpoint created with %s:%d", host, port) def connect( self, protocol_factory: IProtocolFactory) -> "defer.Deferred[IProtocol]": logger.info("Connecting to %s:%i", self.host.decode("ascii"), self.port) return self.ep.connect(protocol_factory)
def main(reactor): from twisted.python import log log.startLogging(sys.stdout) tahoe_dir = "testgrid/alice" cfg = read_config(tahoe_dir, "portnum") token = cfg.get_private_config("api_auth_token").strip() webport = cfg.get_config("node", "web.port") if webport.startswith("tcp:"): port = webport.split(':')[1] else: port = webport factory = WebSocketClientFactory( url=u"ws://127.0.0.1:{}/private/logs/v1".format(port), headers={ "Authorization": "tahoe-lafs {}".format(token), } ) factory.on_open = Deferred() factory.on_close = Deferred() factory.protocol = TahoeLogProtocol endpoint = HostnameEndpoint(reactor, "127.0.0.1", int(port)) try: port = yield endpoint.connect(factory) except ConnectError as e: print("Connection failed: {}".format(e)) return print("port: {}".format(port)) yield factory.on_open print("opened") yield factory.on_close print("closed")
def connect_vrt(scpi): self._scpi = scpi point = HostnameEndpoint(self._reactor, host, VRT_PORT) return point.connect(VRTClientFactory(self._vrt_callback))
def fetch(self): endpoint = HostnameEndpoint(reactor, arguments.host, arguments.port) endpoint.connect(self) self.reactor.run() return self.tcp.cert