Python twisted.internet.endpoints.HostnameEndpoint() Examples

The following are 30 code examples of twisted.internet.endpoints.HostnameEndpoint(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twisted.internet.endpoints , or try the search function .
Example #1
Source File: test_endpoints.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_utf8Encoding(self):
        """
        The hostname passed to L{clientFromString} is treated as utf-8 bytes;
        it is then encoded as IDNA when it is passed along to
        L{HostnameEndpoint}, and passed as unicode to L{optionsForClientTLS}.
        """
        reactor = object()
        endpoint = endpoints.clientFromString(
            reactor, b'tls:\xc3\xa9xample.example.com:443'
        )
        self.assertEqual(
            endpoint._wrappedEndpoint._hostBytes,
            b'xn--xample-9ua.example.com'
        )
        connectionCreator = connectionCreatorFromEndpoint(
            reactor, endpoint)
        self.assertEqual(connectionCreator._hostname,
                         u'\xe9xample.example.com') 
Example #2
Source File: test_endpoints.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_endpointConnectFailure(self):
        """
        When L{HostnameEndpoint.connect} cannot connect to its
        destination, the returned L{Deferred} will fail with
        C{ConnectError}.
        """
        expectedError = error.ConnectError(string="Connection Failed")

        mreactor = RaisingMemoryReactorWithClock(
                connectException=expectedError)

        clientFactory = object()

        ep, ignoredArgs, ignoredDest = self.createClientEndpoint(
            mreactor, clientFactory)

        d = ep.connect(clientFactory)
        mreactor.advance(endpoints.HostnameEndpoint._DEFAULT_ATTEMPT_DELAY)
        self.assertEqual(self.failureResultOf(d).value, expectedError)
        self.assertEqual([], mreactor.getDelayedCalls()) 
Example #3
Source File: test_endpoints.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_fallbackNameResolution(self):
        """
        L{_fallbackNameResolution} returns a L{Deferred} that fires
        with the resoution of the the host and request port.
        """
        from twisted.internet import reactor
        ep = endpoints.HostnameEndpoint(reactor,
                                        host='ignored',
                                        port=0)

        host, port = ("1.2.3.4", 1)

        resolutionDeferred = ep._fallbackNameResolution(host, port)

        def assertHostPortFamilySockType(result):
            self.assertEqual(len(result), 1)
            [(family, socktype, _, _, sockaddr)] = result
            self.assertEqual(family, AF_INET)
            self.assertEqual(socktype, SOCK_STREAM)
            self.assertEqual(sockaddr, (host, port))

        return resolutionDeferred.addCallback(assertHostPortFamilySockType) 
Example #4
Source File: test_endpoints.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_deprecation(self):
        """
        Instantiating L{HostnameEndpoint} with a reactor that does not
        provide L{IReactorPluggableResolver} emits a deprecation warning.
        """
        mreactor = MemoryReactor()

        clientFactory = object()

        self.createClientEndpoint(mreactor, clientFactory)

        warnings = self.flushWarnings()
        self.assertEqual(1, len(warnings))
        self.assertIs(DeprecationWarning, warnings[0]['category'])

        self.assertTrue(warnings[0]['message'].startswith(
            'Passing HostnameEndpoint a reactor that does not provide'
            ' IReactorPluggableNameResolver'
            ' (twisted.internet.testing.MemoryReactorClock)'
            ' was deprecated in Twisted 17.5.0;'
            ' please use a reactor that provides'
            ' IReactorPluggableNameResolver instead')) 
Example #5
Source File: test_endpoints.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_hostIPv6Address(self):
        """
        When the host passed to L{HostnameEndpoint} is an IPv6 address it is
        wrapped in brackets in the string representation, like in a URI. This
        prevents the colon separating the host from the port from being
        ambiguous.
        """
        endpoint = endpoints.HostnameEndpoint(
            deterministicResolvingReactor(Clock(), []),
            b'::1', 22,
        )

        rep = repr(endpoint)

        self.assertEqual("<HostnameEndpoint [::1]:22>", rep)
        self.assertIs(str, type(rep)) 
Example #6
Source File: test_endpoints.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def createClientEndpoint(self, reactor, clientFactory, **connectArgs):
        """
        Creates a L{HostnameEndpoint} instance where the hostname is resolved
        into a single IPv4 address.
        """
        expectedAddress = '1.2.3.4'
        address = HostnameAddress(b"example.com", 80)
        endpoint = endpoints.HostnameEndpoint(
            deterministicResolvingReactor(reactor, [expectedAddress]),
            b"example.com", address.port, **connectArgs
        )

        return (endpoint, (expectedAddress, address.port, clientFactory,
                connectArgs.get('timeout', 30),
                connectArgs.get('bindAddress', None)),
                address) 
Example #7
Source File: test_endpoints.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_utf8Encoding(self):
        """
        The hostname passed to L{clientFromString} is treated as utf-8 bytes;
        it is then encoded as IDNA when it is passed along to
        L{HostnameEndpoint}, and passed as unicode to L{optionsForClientTLS}.
        """
        reactor = object()
        endpoint = endpoints.clientFromString(
            reactor, b'tls:\xc3\xa9xample.example.com:443'
        )
        self.assertEqual(
            endpoint._wrappedEndpoint._hostBytes,
            b'xn--xample-9ua.example.com'
        )
        connectionCreator = connectionCreatorFromEndpoint(
            reactor, endpoint)
        self.assertEqual(connectionCreator._hostname,
                         u'\xe9xample.example.com') 
Example #8
Source File: test_endpoints.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_hostnameEndpointConstruction(self):
        """
        A L{HostnameEndpoint} is constructed from parameters passed to
        L{clientFromString}.
        """
        reactor = object()
        endpoint = endpoints.clientFromString(
            reactor,
            nativeString(
                'tls:example.com:443:timeout=10:bindAddress=127.0.0.1'))
        hostnameEndpoint = endpoint._wrappedEndpoint
        self.assertIs(hostnameEndpoint._reactor, reactor)
        self.assertEqual(hostnameEndpoint._hostBytes, b'example.com')
        self.assertEqual(hostnameEndpoint._port, 443)
        self.assertEqual(hostnameEndpoint._timeout, 10)
        self.assertEqual(hostnameEndpoint._bindAddress,
                         nativeString('127.0.0.1')) 
Example #9
Source File: test_endpoints.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_ignoreUnknownAddressTypes(self):
        """
        If an address type other than L{IPv4Address} and L{IPv6Address} is
        returned by on address resolution, the endpoint ignores that address.
        """
        self.mreactor = MemoryReactor()
        self.endpoint = endpoints.HostnameEndpoint(
            deterministicResolvingReactor(self.mreactor, ['1.2.3.4', object(),
                                                          '1:2::3:4']),
            b"www.example.com", 80
        )
        clientFactory = None

        self.endpoint.connect(clientFactory)

        self.mreactor.advance(0.3)
        (host, port, factory, timeout, bindAddress) = self.mreactor.tcpClients[1]
        self.assertEqual(len(self.mreactor.tcpClients), 2)
        self.assertEqual(host, '1:2::3:4')
        self.assertEqual(port, 80) 
Example #10
Source File: test_endpoints.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def connectionCreatorFromEndpoint(memoryReactor, tlsEndpoint):
    """
    Given a L{MemoryReactor} and the result of calling L{wrapClientTLS},
    extract the L{IOpenSSLClientConnectionCreator} associated with it.

    Implementation presently uses private attributes but could (and should) be
    refactored to just call C{.connect()} on the endpoint, when
    L{HostnameEndpoint} starts directing its C{getaddrinfo} call through the
    reactor it is passed somehow rather than via the global threadpool.

    @param memoryReactor: the reactor attached to the given endpoint.
        (Presently unused, but included so tests won't need to be modified to
        honor it.)

    @param tlsEndpoint: The result of calling L{wrapClientTLS}.

    @return: the client connection creator associated with the endpoint
        wrapper.
    @rtype: L{IOpenSSLClientConnectionCreator}
    """
    return tlsEndpoint._wrapperFactory(None)._connectionCreator 
Example #11
Source File: test_endpoints.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_hostnameEndpointConstruction(self):
        """
        A L{HostnameEndpoint} is constructed from parameters passed to
        L{clientFromString}.
        """
        reactor = object()
        endpoint = endpoints.clientFromString(
            reactor,
            nativeString(
                'tls:example.com:443:timeout=10:bindAddress=127.0.0.1'))
        hostnameEndpoint = endpoint._wrappedEndpoint
        self.assertIs(hostnameEndpoint._reactor, reactor)
        self.assertEqual(hostnameEndpoint._hostBytes, b'example.com')
        self.assertEqual(hostnameEndpoint._port, 443)
        self.assertEqual(hostnameEndpoint._timeout, 10)
        self.assertEqual(hostnameEndpoint._bindAddress,
                         nativeString('127.0.0.1')) 
Example #12
Source File: test_endpoints.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_deferBadEncodingToConnect(self):
        """
        Since any client of L{IStreamClientEndpoint} needs to handle Deferred
        failures from C{connect}, L{HostnameEndpoint}'s constructor will not
        raise exceptions when given bad host names, instead deferring to
        returning a failing L{Deferred} from C{connect}.
        """
        endpoint = endpoints.HostnameEndpoint(
            deterministicResolvingReactor(MemoryReactor(), ['127.0.0.1']),
            b'\xff-garbage-\xff', 80
        )
        deferred = endpoint.connect(Factory.forProtocol(Protocol))
        err = self.failureResultOf(deferred, ValueError)
        self.assertIn("\\xff-garbage-\\xff", str(err))
        endpoint = endpoints.HostnameEndpoint(
            deterministicResolvingReactor(MemoryReactor(), ['127.0.0.1']),
            u'\u2ff0-garbage-\u2ff0', 80
        )
        deferred = endpoint.connect(Factory())
        err = self.failureResultOf(deferred, ValueError)
        self.assertIn("\\u2ff0-garbage-\\u2ff0", str(err)) 
Example #13
Source File: test_endpoints.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_endpointConnectFailure(self):
        """
        If L{HostnameEndpoint.connect} is invoked and there is no server
        listening for connections, the returned L{Deferred} will fail with
        C{ConnectError}.
        """
        expectedError = error.ConnectError(string="Connection Failed")

        mreactor = RaisingMemoryReactorWithClock(
                connectException=expectedError)

        clientFactory = object()

        ep, ignoredArgs, ignoredDest = self.createClientEndpoint(
            mreactor, clientFactory)

        d = ep.connect(clientFactory)
        mreactor.advance(endpoints.HostnameEndpoint._DEFAULT_ATTEMPT_DELAY)
        self.assertEqual(self.failureResultOf(d).value, expectedError)
        self.assertEqual([], mreactor.getDelayedCalls()) 
Example #14
Source File: test_endpoints.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_endpointConnectFailureAfterIteration(self):
        """
        If a connection attempt initiated by
        L{HostnameEndpoint.connect} fails only after
        L{HostnameEndpoint} has exhausted the list of possible server
        addresses, the returned L{Deferred} will fail with
        C{ConnectError}.
        """
        expectedError = error.ConnectError(string="Connection Failed")

        mreactor = MemoryReactor()

        clientFactory = object()

        ep, ignoredArgs, ignoredDest = self.createClientEndpoint(
            mreactor, clientFactory)

        d = ep.connect(clientFactory)
        mreactor.advance(0.3)
        host, port, factory, timeout, bindAddress = mreactor.tcpClients[0]
        factory.clientConnectionFailed(mreactor.connectors[0], expectedError)
        self.assertEqual(self.failureResultOf(d).value, expectedError)
        self.assertEqual([], mreactor.getDelayedCalls()) 
Example #15
Source File: test_endpoints.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_endpointConnectFailureAfterIteration(self):
        """
        If a connection attempt initiated by
        L{HostnameEndpoint.connect} fails only after
        L{HostnameEndpoint} has exhausted the list of possible server
        addresses, the returned L{Deferred} will fail with
        C{ConnectError}.
        """
        expectedError = error.ConnectError(string="Connection Failed")

        mreactor = MemoryReactor()

        clientFactory = object()

        ep, ignoredArgs, ignoredDest = self.createClientEndpoint(
            mreactor, clientFactory)

        d = ep.connect(clientFactory)
        mreactor.advance(0.3)
        host, port, factory, timeout, bindAddress = mreactor.tcpClients[0]
        factory.clientConnectionFailed(mreactor.connectors[0], expectedError)
        self.assertEqual(self.failureResultOf(d).value, expectedError)
        self.assertEqual([], mreactor.getDelayedCalls()) 
Example #16
Source File: test_endpoints.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_endpointConnectFailure(self):
        """
        If L{HostnameEndpoint.connect} is invoked and there is no server
        listening for connections, the returned L{Deferred} will fail with
        C{ConnectError}.
        """
        expectedError = error.ConnectError(string="Connection Failed")

        mreactor = RaisingMemoryReactorWithClock(
                connectException=expectedError)

        clientFactory = object()

        ep, ignoredArgs, ignoredDest = self.createClientEndpoint(
            mreactor, clientFactory)

        d = ep.connect(clientFactory)
        mreactor.advance(endpoints.HostnameEndpoint._DEFAULT_ATTEMPT_DELAY)
        self.assertEqual(self.failureResultOf(d).value, expectedError)
        self.assertEqual([], mreactor.getDelayedCalls()) 
Example #17
Source File: test_endpoints.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def createClientEndpoint(self, reactor, clientFactory, **connectArgs):
        """
        Creates a L{HostnameEndpoint} instance where the hostname is resolved
        into a single IPv4 address.
        """
        expectedAddress = '1.2.3.4'
        address = HostnameAddress(b"example.com", 80)
        endpoint = endpoints.HostnameEndpoint(
            deterministicResolvingReactor(reactor, [expectedAddress]),
            b"example.com", address.port, **connectArgs
        )

        return (endpoint, (expectedAddress, address.port, clientFactory,
                connectArgs.get('timeout', 30),
                connectArgs.get('bindAddress', None)),
                address) 
Example #18
Source File: test_endpoints.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_deferBadEncodingToConnect(self):
        """
        Since any client of L{IStreamClientEndpoint} needs to handle Deferred
        failures from C{connect}, L{HostnameEndpoint}'s constructor will not
        raise exceptions when given bad host names, instead deferring to
        returning a failing L{Deferred} from C{connect}.
        """
        endpoint = endpoints.HostnameEndpoint(
            deterministicResolvingReactor(MemoryReactor(), ['127.0.0.1']),
            b'\xff-garbage-\xff', 80
        )
        deferred = endpoint.connect(Factory.forProtocol(Protocol))
        err = self.failureResultOf(deferred, ValueError)
        self.assertIn("\\xff-garbage-\\xff", str(err))
        endpoint = endpoints.HostnameEndpoint(
            deterministicResolvingReactor(MemoryReactor(), ['127.0.0.1']),
            u'\u2ff0-garbage-\u2ff0', 80
        )
        deferred = endpoint.connect(Factory())
        err = self.failureResultOf(deferred, ValueError)
        self.assertIn("\\u2ff0-garbage-\\u2ff0", str(err)) 
Example #19
Source File: test_hints.py    From magic-wormhole with MIT License 6 votes vote down vote up
def test_endpoint_from_hint_obj(self):
        def efho(hint, tor=None):
            return endpoint_from_hint_obj(hint, tor, reactor)
        self.assertIsInstance(efho(DirectTCPV1Hint("host", 1234, 0.0)),
                              endpoints.HostnameEndpoint)
        self.assertEqual(efho("unknown:stuff:yowza:pivlor"), None)

        # tor=None
        self.assertEqual(efho(TorTCPV1Hint("host", "port", 0)), None)
        self.assertEqual(efho(UnknownHint("foo")), None)

        tor = mock.Mock()
        def tor_ep(hostname, port):
            if hostname == "non-public":
                raise ValueError
            return ("tor_ep", hostname, port)
        tor.stream_via = mock.Mock(side_effect=tor_ep)

        self.assertEqual(efho(DirectTCPV1Hint("host", 1234, 0.0), tor),
                         ("tor_ep", "host", 1234))
        self.assertEqual(efho(TorTCPV1Hint("host2.onion", 1234, 0.0), tor),
                         ("tor_ep", "host2.onion", 1234))
        self.assertEqual( efho(DirectTCPV1Hint("non-public", 1234, 0.0), tor), None)

        self.assertEqual(efho(UnknownHint("foo"), tor), None) 
Example #20
Source File: _hints.py    From magic-wormhole with MIT License 6 votes vote down vote up
def endpoint_from_hint_obj(hint, tor, reactor):
    if tor:
        if isinstance(hint, (DirectTCPV1Hint, TorTCPV1Hint)):
            # this Tor object will throw ValueError for non-public IPv4
            # addresses and any IPv6 address
            try:
                return tor.stream_via(hint.hostname, hint.port)
            except ValueError:
                return None
        return None
    if isinstance(hint, DirectTCPV1Hint):
        # avoid DNS lookup unless necessary
        if isIPAddress(hint.hostname):
            return TCP4ClientEndpoint(reactor, hint.hostname, hint.port)
        if isIPv6Address(hint.hostname):
            return TCP6ClientEndpoint(reactor, hint.hostname, hint.port)
        return HostnameEndpoint(reactor, hint.hostname, hint.port)
    return None 
Example #21
Source File: test_endpoints.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_nonNormalizedText(self):
        """
        A L{HostnameEndpoint} constructed with NFD-normalized text will store
        the NFC-normalized version of that text.
        """
        endpoint = endpoints.HostnameEndpoint(
            deterministicResolvingReactor(MemoryReactor(), ['127.0.0.1']),
            normalize('NFD', self.sampleIDNAText), 80
        )
        self.assertEqual(endpoint._hostBytes, self.sampleIDNABytes)
        self.assertEqual(endpoint._hostText, self.sampleIDNAText) 
Example #22
Source File: test_endpoints.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_idnaHostname(self):
        """
        When IDN is passed to the L{HostnameEndpoint} constructor the string
        representation includes the punycode version of the host.
        """
        endpoint = endpoints.HostnameEndpoint(
            deterministicResolvingReactor(Clock(), []),
            u'b\xfccher.ch', 443,
        )

        rep = repr(endpoint)

        self.assertEqual("<HostnameEndpoint xn--bcher-kva.ch:443>", rep)
        self.assertIs(str, type(rep)) 
Example #23
Source File: test_endpoints.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def createClientEndpoint(self, reactor, clientFactory, **connectArgs):
        """
        Creates a L{HostnameEndpoint} instance where the hostname is
        resolved into a single IPv6 address.

        @param reactor: The L{MemoryReactor}

        @param clientFactory: The client L{IProtocolFactory}

        @param connectArgs: Additional arguments to
            L{HostnameEndpoint.connect}

        @return: A L{tuple} of the form C{(endpoint, (expectedAddress,
            expectedPort, clientFactory, timeout, localBindAddress,
            hostnameAddress))}
        """
        expectedAddress = '1:2::3:4'
        address = HostnameAddress(b"ipv6.example.com", 80)
        endpoint = endpoints.HostnameEndpoint(
            reactor, b"ipv6.example.com", address.port, **connectArgs
        )

        def fakegetaddrinfo(host, port, family, socktype):
            return [
                (AF_INET6, SOCK_STREAM, IPPROTO_TCP, '',
                 (expectedAddress, 80)),
            ]

        endpoint._getaddrinfo = fakegetaddrinfo
        endpoint._deferToThread = self.synchronousDeferredToThread

        return (endpoint, (expectedAddress, address.port, clientFactory,
                connectArgs.get('timeout', 30),
                connectArgs.get('bindAddress', None)),
                address) 
Example #24
Source File: test_endpoints.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_badEncoding(self):
        """
        When a bad hostname is passed to L{HostnameEndpoint}, the string
        representation displays invalid characters in backslash-escaped form.
        """
        endpoint = endpoints.HostnameEndpoint(
            deterministicResolvingReactor(Clock(), []),
            b'\xff-garbage-\xff', 80
        )

        self.assertEqual(
            '<HostnameEndpoint \\xff-garbage-\\xff:80>',
            repr(endpoint),
        ) 
Example #25
Source File: test_endpoints.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_failure(self):
        """
        If no address is returned by GAI for a hostname, the connection attempt
        fails with L{error.DNSLookupError}.
        """
        endpoint = endpoints.HostnameEndpoint(
            deterministicResolvingReactor(Clock(), []),
            b"example.com", 80
        )
        clientFactory = object()
        dConnect = endpoint.connect(clientFactory)
        exc = self.failureResultOf(dConnect, error.DNSLookupError)
        self.assertIn("example.com", str(exc)) 
Example #26
Source File: test_endpoints.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def setUp(self):
        self.mreactor = MemoryReactor()
        self.endpoint = endpoints.HostnameEndpoint(
            deterministicResolvingReactor(self.mreactor,
                                          ['1.2.3.4', '1:2::3:4']),
            b"www.example.com", 80) 
Example #27
Source File: forwarding.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def channelOpen(self, specificData):
        """
        See: L{channel.SSHChannel}
        """
        log.msg("connecting to %s:%i" % self.hostport)
        ep = HostnameEndpoint(
            self._reactor, self.hostport[0], self.hostport[1])
        d = connectProtocol(ep, SSHForwardingClient(self))
        d.addCallbacks(self._setClient, self._close)
        self._channelOpenDeferred = d 
Example #28
Source File: matrixfederationagent.py    From sydent with Apache License 2.0 5 votes vote down vote up
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) 
Example #29
Source File: pyrdp-clonecert.py    From pyrdp with GNU General Public License v3.0 5 votes vote down vote up
def fetch(self):
        endpoint = HostnameEndpoint(reactor, self.host, self.port)
        endpoint.connect(self)
        self.reactor.run()

        return self.tcp.cert 
Example #30
Source File: test_cli.py    From magic-wormhole with MIT License 5 votes vote down vote up
def stream_via(self, host, port, tls=False):
        self.endpoints.append((host, port, tls))
        return endpoints.HostnameEndpoint(reactor, host, port)