Here are the examples of the python api twisted.internet.endpoints.HostnameEndpoint taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
24 Examples
3
Source : forwarding.py
with MIT License
from autofelix
with MIT License
from autofelix
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
def _setClient(self, client):
3
Source : test_endpoints.py
with MIT License
from autofelix
with MIT License
from autofelix
def test_idnaHostnameText(self):
"""
A L{HostnameEndpoint} constructed with text will contain an
IDNA-encoded bytes representation of that text.
"""
endpoint = endpoints.HostnameEndpoint(
deterministicResolvingReactor(MemoryReactor(), ['127.0.0.1']),
self.sampleIDNAText, 80
)
self.assertEqual(endpoint._hostBytes, self.sampleIDNABytes)
self.assertEqual(endpoint._hostText, self.sampleIDNAText)
def test_idnaHostnameBytes(self):
3
Source : test_endpoints.py
with MIT License
from autofelix
with MIT License
from autofelix
def test_idnaHostnameBytes(self):
"""
A L{HostnameEndpoint} constructed with bytes will contain an
IDNA-decoded textual representation of those bytes.
"""
endpoint = endpoints.HostnameEndpoint(
deterministicResolvingReactor(MemoryReactor(), ['127.0.0.1']),
self.sampleIDNAText, 80
)
self.assertEqual(endpoint._hostBytes, self.sampleIDNABytes)
self.assertEqual(endpoint._hostText, self.sampleIDNAText)
def test_nonNormalizedText(self):
3
Source : test_endpoints.py
with MIT License
from autofelix
with MIT License
from autofelix
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)
def test_deferBadEncodingToConnect(self):
3
Source : test_endpoints.py
with MIT License
from autofelix
with MIT License
from autofelix
def test_allASCII(self):
"""
The string representation of L{HostnameEndpoint} includes the host and
port passed to the constructor.
"""
endpoint = endpoints.HostnameEndpoint(
deterministicResolvingReactor(Clock(), []),
'example.com', 80,
)
rep = repr(endpoint)
self.assertEqual(" < HostnameEndpoint example.com:80>", rep)
self.assertIs(str, type(rep))
def test_idnaHostname(self):
3
Source : test_endpoints.py
with MIT License
from autofelix
with MIT License
from autofelix
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))
def test_hostIPv6Address(self):
3
Source : test_endpoints.py
with MIT License
from autofelix
with MIT License
from autofelix
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))
def test_badEncoding(self):
3
Source : test_endpoints.py
with MIT License
from autofelix
with MIT License
from autofelix
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),
)
class HostnameEndpointsGAIFailureTests(unittest.TestCase):
3
Source : test_endpoints.py
with MIT License
from autofelix
with MIT License
from autofelix
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))
class HostnameEndpointsFasterConnectionTests(unittest.TestCase):
3
Source : test_endpoints.py
with MIT License
from autofelix
with MIT License
from autofelix
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)
def test_ignoreUnknownAddressTypes(self):
3
Source : test_endpoints.py
with MIT License
from fbla-competitive-events
with MIT License
from fbla-competitive-events
def setUp(self):
self.mreactor = MemoryReactor()
self.endpoint = endpoints.HostnameEndpoint(self.mreactor,
b"www.example.com", 80)
def nameResolution(host, port):
self.assertEqual(b"www.example.com", host)
data = [
(AF_INET, SOCK_STREAM, IPPROTO_TCP, '', ('1.2.3.4', port)),
(AF_INET6, SOCK_STREAM, IPPROTO_TCP, '', ('1:2::3:4', port, 0, 0))
]
return defer.succeed(data)
self.endpoint._nameResolution = nameResolution
def test_ignoreUnknownAddressFamilies(self):
3
Source : pyrdp-clonecert.py
with GNU General Public License v3.0
from GoSecure
with GNU General Public License v3.0
from GoSecure
def fetch(self):
endpoint = HostnameEndpoint(reactor, self.host, self.port)
endpoint.connect(self)
self.reactor.run()
return self.tcp.cert
def sendConnectionRequest(self):
0
Source : test_endpoints.py
with MIT License
from autofelix
with MIT License
from autofelix
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)
class _HostnameEndpointMemoryReactorMixin(ClientEndpointTestCaseMixin):
0
Source : test_endpoints.py
with MIT License
from autofelix
with MIT License
from autofelix
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))
class HostnameEndpointReprTests(unittest.SynchronousTestCase):
0
Source : test_endpoints.py
with MIT License
from autofelix
with MIT License
from autofelix
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)
def test_IPv4IsFaster(self):
0
Source : client.py
with MIT License
from autofelix
with MIT License
from autofelix
def endpointForURI(self, uri):
"""
Connect directly over TCP for C{b'http'} scheme, and TLS for
C{b'https'}.
@param uri: L{URI} to connect to.
@return: Endpoint to connect to.
@rtype: L{IStreamClientEndpoint}
"""
kwargs = {}
if self._connectTimeout is not None:
kwargs['timeout'] = self._connectTimeout
kwargs['bindAddress'] = self._bindAddress
try:
host = nativeString(uri.host)
except UnicodeDecodeError:
raise ValueError(("The host of the provided URI ({uri.host!r}) "
"contains non-ASCII octets, it should be ASCII "
"decodable.").format(uri=uri))
endpoint = HostnameEndpoint(self._reactor, host, uri.port, **kwargs)
if uri.scheme == b'http':
return endpoint
elif uri.scheme == b'https':
connectionCreator = self._policyForHTTPS.creatorForNetloc(uri.host,
uri.port)
return wrapClientTLS(connectionCreator, endpoint)
else:
raise SchemeNotSupported("Unsupported scheme: %r" % (uri.scheme,))
@implementer(IAgent)
0
Source : test_endpoints.py
with MIT License
from fbla-competitive-events
with MIT License
from fbla-competitive-events
def test_nameResolution(self):
"""
While resolving hostnames, _nameResolution calls _deferToThread with
_getaddrinfo.
"""
calls = []
clientFactory = object()
def fakeDeferToThread(f, *args, **kwargs):
calls.append((f, args, kwargs))
return defer.Deferred()
endpoint = endpoints.HostnameEndpoint(reactor, b'ipv4.example.com',
1234)
fakegetaddrinfo = object()
endpoint._getaddrinfo = fakegetaddrinfo
endpoint._deferToThread = fakeDeferToThread
endpoint.connect(clientFactory)
self.assertEqual(
[(fakegetaddrinfo, (b"ipv4.example.com", 1234, 0, SOCK_STREAM),
{})], calls)
class HostnameEndpointsOneIPv6Tests(ClientEndpointTestCaseMixin,
0
Source : test_endpoints.py
with MIT License
from fbla-competitive-events
with MIT License
from fbla-competitive-events
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(Clock(), b"example.com", 80)
def testNameResolution(host, port):
self.assertEqual(b"example.com", host)
data = error.DNSLookupError("Problems")
return defer.fail(data)
endpoint._nameResolution = testNameResolution
clientFactory = object()
dConnect = endpoint.connect(clientFactory)
return self.assertFailure(dConnect, error.DNSLookupError)
class HostnameEndpointsFasterConnectionTests(unittest.TestCase):
0
Source : test_endpoints.py
with MIT License
from fbla-competitive-events
with MIT License
from fbla-competitive-events
def test_ignoreUnknownAddressFamilies(self):
"""
If an address family other than AF_INET and AF_INET6 is returned by
on address resolution, the endpoint ignores that address.
"""
self.mreactor = MemoryReactor()
self.endpoint = endpoints.HostnameEndpoint(self.mreactor,
b"www.example.com", 80)
AF_INX = None # An arbitrary name for testing
def nameResolution(host, port):
self.assertEqual(b"www.example.com", host)
data = [
(AF_INET, SOCK_STREAM, IPPROTO_TCP, '', ('1.2.3.4', port)),
(AF_INX, SOCK_STREAM, 'SOME_PROTOCOL_IN_FUTURE', '',
('a.b.c.d', port)),
(AF_INET6, SOCK_STREAM, IPPROTO_TCP, '', ('1:2::3:4', port, 0,
0))]
return defer.succeed(data)
self.endpoint._nameResolution = nameResolution
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)
def test_IPv4IsFaster(self):
0
Source : test_endpoints.py
with Apache License 2.0
from lynings
with Apache License 2.0
from lynings
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))
class HostnameEndpointsGAIFailureTests(unittest.TestCase):
0
Source : matrix_federation_agent.py
with Apache License 2.0
from matrix-org
with Apache License 2.0
from matrix-org
async def _do_connect(self, protocol_factory: IProtocolFactory) -> IProtocol:
first_exception = None
server_list = await self._resolve_server()
for server in server_list:
host = server.host
port = server.port
should_skip_proxy = False
if self.no_proxy is not None:
should_skip_proxy = proxy_bypass_environment(
host.decode(),
proxies={"no": self.no_proxy},
)
endpoint: IStreamClientEndpoint
try:
if self._https_proxy_endpoint and not should_skip_proxy:
logger.debug(
"Connecting to %s:%i via %s",
host.decode("ascii"),
port,
self._https_proxy_endpoint,
)
endpoint = HTTPConnectProxyEndpoint(
self._reactor,
self._https_proxy_endpoint,
host,
port,
proxy_creds=self._https_proxy_creds,
)
else:
logger.debug("Connecting to %s:%i", host.decode("ascii"), port)
# not using a proxy
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,))
async def _resolve_server(self) -> List[Server]:
0
Source : proxyagent.py
with Apache License 2.0
from matrix-org
with Apache License 2.0
from matrix-org
def request(
self,
method: bytes,
uri: bytes,
headers: Optional[Headers] = None,
bodyProducer: Optional[IBodyProducer] = None,
) -> defer.Deferred:
"""
Issue a request to the server indicated by the given uri.
Supports `http` and `https` schemes.
An existing connection from the connection pool may be used or a new one may be
created.
See also: twisted.web.iweb.IAgent.request
Args:
method: The request method to use, such as `GET`, `POST`, etc
uri: The location of the resource to request.
headers: Extra headers to send with the request
bodyProducer: An object which can generate bytes to make up the body of
this request (for example, the properly encoded contents of a file for
a file upload). Or, None if the request is to have no body.
Returns:
Deferred[IResponse]: completes when the header of the response has
been received (regardless of the response status code).
Can fail with:
SchemeNotSupported: if the uri is not http or https
twisted.internet.error.TimeoutError if the server we are connecting
to (proxy or destination) does not accept a connection before
connectTimeout.
... other things too.
"""
uri = uri.strip()
if not _VALID_URI.match(uri):
raise ValueError(f"Invalid URI {uri!r}")
parsed_uri = URI.fromBytes(uri)
pool_key = f"{parsed_uri.scheme!r}{parsed_uri.host!r}{parsed_uri.port}"
request_path = parsed_uri.originForm
should_skip_proxy = False
if self.no_proxy is not None:
should_skip_proxy = proxy_bypass_environment(
parsed_uri.host.decode(),
proxies={"no": self.no_proxy},
)
if (
parsed_uri.scheme == b"http"
and self.http_proxy_endpoint
and not should_skip_proxy
):
# Determine whether we need to set Proxy-Authorization headers
if self.http_proxy_creds:
# Set a Proxy-Authorization header
if headers is None:
headers = Headers()
headers.addRawHeader(
b"Proxy-Authorization",
self.http_proxy_creds.as_proxy_authorization_value(),
)
# Cache *all* connections under the same key, since we are only
# connecting to a single destination, the proxy:
pool_key = "http-proxy"
endpoint = self.http_proxy_endpoint
request_path = uri
elif (
parsed_uri.scheme == b"https"
and self.https_proxy_endpoint
and not should_skip_proxy
):
endpoint = HTTPConnectProxyEndpoint(
self.proxy_reactor,
self.https_proxy_endpoint,
parsed_uri.host,
parsed_uri.port,
self.https_proxy_creds,
)
else:
# not using a proxy
endpoint = HostnameEndpoint(
self._reactor, parsed_uri.host, parsed_uri.port, **self._endpoint_kwargs
)
logger.debug("Requesting %s via %s", uri, endpoint)
if parsed_uri.scheme == b"https":
tls_connection_creator = self._policy_for_https.creatorForNetloc(
parsed_uri.host, parsed_uri.port
)
endpoint = wrapClientTLS(tls_connection_creator, endpoint)
elif parsed_uri.scheme == b"http":
pass
else:
return defer.fail(
Failure(
SchemeNotSupported("Unsupported scheme: %r" % (parsed_uri.scheme,))
)
)
return self._requestWithEndpoint(
pool_key, endpoint, method, parsed_uri, headers, bodyProducer, request_path
)
def http_proxy_endpoint(
0
Source : proxyagent.py
with Apache License 2.0
from matrix-org
with Apache License 2.0
from matrix-org
def http_proxy_endpoint(
proxy: Optional[bytes],
reactor: IReactorCore,
tls_options_factory: Optional[IPolicyForHTTPS],
**kwargs,
) -> Tuple[Optional[IStreamClientEndpoint], Optional[ProxyCredentials]]:
"""Parses an http proxy setting and returns an endpoint for the proxy
Args:
proxy: the proxy setting in the form: [scheme://][ < username>: < password>@] < host>[: < port>]
This currently supports http:// and https:// proxies.
A hostname without scheme is assumed to be http.
reactor: reactor to be used to connect to the proxy
tls_options_factory: the TLS options to use when connecting through a https proxy
kwargs: other args to be passed to HostnameEndpoint
Returns:
a tuple of
endpoint to use to connect to the proxy, or None
ProxyCredentials or if no credentials were found, or None
Raise:
ValueError if proxy has no hostname or unsupported scheme.
RuntimeError if no tls_options_factory is given for a https connection
"""
if proxy is None:
return None, None
# Note: urlsplit/urlparse cannot be used here as that does not work (for Python
# 3.9+) on scheme-less proxies, e.g. host:port.
scheme, host, port, credentials = parse_proxy(proxy)
proxy_endpoint = HostnameEndpoint(reactor, host, port, **kwargs)
if scheme == b"https":
if tls_options_factory:
tls_options = tls_options_factory.creatorForNetloc(host, port)
proxy_endpoint = wrapClientTLS(tls_options, proxy_endpoint)
else:
raise RuntimeError(
f"No TLS options for a https connection via proxy {proxy!s}"
)
return proxy_endpoint, credentials
def parse_proxy(
0
Source : _remote.py
with Apache License 2.0
from matrix-org
with Apache License 2.0
from matrix-org
def __init__(
self,
host: str,
port: int,
maximum_buffer: int = 1000,
level=logging.NOTSET,
_reactor=None,
):
super().__init__(level=level)
self.host = host
self.port = port
self.maximum_buffer = maximum_buffer
self._buffer: Deque[logging.LogRecord] = deque()
self._connection_waiter: Optional[Deferred] = None
self._producer: Optional[LogProducer] = None
# Connect without DNS lookups if it's a direct IP.
if _reactor is None:
from twisted.internet import reactor
_reactor = reactor
try:
ip = ip_address(self.host)
if isinstance(ip, IPv4Address):
endpoint: IStreamClientEndpoint = TCP4ClientEndpoint(
_reactor, self.host, self.port
)
elif isinstance(ip, IPv6Address):
endpoint = TCP6ClientEndpoint(_reactor, self.host, self.port)
else:
raise ValueError("Unknown IP address provided: %s" % (self.host,))
except ValueError:
endpoint = HostnameEndpoint(_reactor, self.host, self.port)
factory = Factory.forProtocol(Protocol)
self._service = ClientService(endpoint, factory, clock=_reactor)
self._service.startService()
self._stopping = False
self._connect()
def close(self):