本文整理汇总了Python中twisted.internet.endpoints.TCP4ClientEndpoint方法的典型用法代码示例。如果您正苦于以下问题:Python endpoints.TCP4ClientEndpoint方法的具体用法?Python endpoints.TCP4ClientEndpoint怎么用?Python endpoints.TCP4ClientEndpoint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.endpoints
的用法示例。
在下文中一共展示了endpoints.TCP4ClientEndpoint方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: connect
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import TCP4ClientEndpoint [as 别名]
def connect(self, reconnecting=False):
if self.connecting and not reconnecting:
return
self.connecting = True
end_point = TCP4ClientEndpoint(reactor, self.host, self.port, 10)
d = end_point.connect(Factory.forProtocol(GraphiteProtocol))
def success(connection):
self.connecting = False
log.info('Connected to {replica}', replica=self)
self.connection = connection
def failed(error):
log.error('Connect to {replica} failed: {error}', replica=self, error=error)
reactor.callLater(10, self.connect, True)
d.addCallbacks(success, failed)
示例2: createClientEndpoint
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import TCP4ClientEndpoint [as 别名]
def createClientEndpoint(self, reactor, clientFactory, **connectArgs):
"""
Create an L{TCP4ClientEndpoint} and return the values needed to verify
its behavior.
@param reactor: A fake L{IReactorTCP} that L{TCP4ClientEndpoint} can
call L{IReactorTCP.connectTCP} on.
@param clientFactory: The thing that we expect to be passed to our
L{IStreamClientEndpoint.connect} implementation.
@param connectArgs: Optional dictionary of arguments to
L{IReactorTCP.connectTCP}
"""
address = IPv4Address("TCP", "localhost", 80)
return (endpoints.TCP4ClientEndpoint(reactor,
address.host,
address.port,
**connectArgs),
(address.host, address.port, clientFactory,
connectArgs.get('timeout', 30),
connectArgs.get('bindAddress', None)),
address)
示例3: test_tcp
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import TCP4ClientEndpoint [as 别名]
def test_tcp(self):
"""
When passed a TCP strports description, L{endpoints.clientFromString}
returns a L{TCP4ClientEndpoint} instance initialized with the values
from the string.
"""
reactor = object()
client = endpoints.clientFromString(
reactor,
"tcp:host=example.com:port=1234:timeout=7:bindAddress=10.0.0.2")
self.assertIsInstance(client, endpoints.TCP4ClientEndpoint)
self.assertIs(client._reactor, reactor)
self.assertEqual(client._host, "example.com")
self.assertEqual(client._port, 1234)
self.assertEqual(client._timeout, 7)
self.assertEqual(client._bindAddress, ("10.0.0.2", 0))
示例4: test_tcpPositionalArgs
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import TCP4ClientEndpoint [as 别名]
def test_tcpPositionalArgs(self):
"""
When passed a TCP strports description using positional arguments,
L{endpoints.clientFromString} returns a L{TCP4ClientEndpoint} instance
initialized with the values from the string.
"""
reactor = object()
client = endpoints.clientFromString(
reactor,
"tcp:example.com:1234:timeout=7:bindAddress=10.0.0.2")
self.assertIsInstance(client, endpoints.TCP4ClientEndpoint)
self.assertIs(client._reactor, reactor)
self.assertEqual(client._host, "example.com")
self.assertEqual(client._port, 1234)
self.assertEqual(client._timeout, 7)
self.assertEqual(client._bindAddress, ("10.0.0.2", 0))
示例5: test_connectProtocolCreatesFactory
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import TCP4ClientEndpoint [as 别名]
def test_connectProtocolCreatesFactory(self):
"""
C{endpoints.connectProtocol} calls the given endpoint's C{connect()}
method with a factory that will build the given protocol.
"""
reactor = MemoryReactor()
endpoint = endpoints.TCP4ClientEndpoint(reactor, "127.0.0.1", 0)
theProtocol = object()
endpoints.connectProtocol(endpoint, theProtocol)
# A TCP connection was made via the given endpoint:
self.assertEqual(len(reactor.tcpClients), 1)
# TCP4ClientEndpoint uses a _WrapperFactory around the underlying
# factory, so we need to unwrap it:
factory = reactor.tcpClients[0][2]._wrappedFactory
self.assertIsInstance(factory, protocol.Factory)
self.assertIs(factory.buildProtocol(None), theProtocol)
示例6: secureConnection
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import TCP4ClientEndpoint [as 别名]
def secureConnection(self):
"""
Create and return a new SSH connection which has been secured and on
which authentication has already happened.
@return: A L{Deferred} which fires with the ready-to-use connection or
with a failure if something prevents the connection from being
setup, secured, or authenticated.
"""
protocol = _CommandTransport(self)
ready = protocol.connectionReady
sshClient = TCP4ClientEndpoint(
self.reactor, nativeString(self.hostname), self.port)
d = connectProtocol(sshClient, protocol)
d.addCallback(lambda ignored: ready)
return d
示例7: connectableEndpoint
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import TCP4ClientEndpoint [as 别名]
def connectableEndpoint(debug=False):
"""
Create an endpoint that can be fired on demand.
@param debug: A flag; whether to dump output from the established
connection to stdout.
@type debug: L{bool}
@return: A client endpoint, and an object that will cause one of the
L{Deferred}s returned by that client endpoint.
@rtype: 2-L{tuple} of (L{IStreamClientEndpoint}, L{ConnectionCompleter})
"""
reactor = MemoryReactorClock()
clientEndpoint = TCP4ClientEndpoint(reactor, "0.0.0.0", 4321)
serverEndpoint = TCP4ServerEndpoint(reactor, 4321)
serverEndpoint.listen(Factory.forProtocol(Protocol))
return clientEndpoint, ConnectionCompleter(reactor)
示例8: broadcast
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import TCP4ClientEndpoint [as 别名]
def broadcast(small_dir, data):
d_list = []
responses = []
# d = defer.Deferred()
def gotProtocol(p):
p.sendLine(data)
for (kid, ip, port) in small_dir:
_stats[ip] += 1
point = TCP4ClientEndpoint(reactor, ip, int(port), timeout=10)
f = RSCfactory()
d = point.connect(f)
d.addCallback(gotProtocol)
d.addErrback(f.d.errback)
# f.d.errback
d_list += [ f.d ]
d_all = defer.gatherResults(d_list)
return d_all
示例9: endpoint_from_hint_obj
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import TCP4ClientEndpoint [as 别名]
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
示例10: test_tcp
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import TCP4ClientEndpoint [as 别名]
def test_tcp(self):
"""
When passed a TCP strports description, L{endpointClient} returns a
L{TCP4ClientEndpoint} instance initialized with the values from the
string.
"""
reactor = object()
client = endpoints.clientFromString(
reactor,
"tcp:host=example.com:port=1234:timeout=7:bindAddress=10.0.0.2")
self.assertIsInstance(client, endpoints.TCP4ClientEndpoint)
self.assertIdentical(client._reactor, reactor)
self.assertEquals(client._host, "example.com")
self.assertEquals(client._port, 1234)
self.assertEquals(client._timeout, 7)
self.assertEquals(client._bindAddress, "10.0.0.2")
示例11: _create_client_service
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import TCP4ClientEndpoint [as 别名]
def _create_client_service(self, nodeurl, api_token):
url = parse(nodeurl)
wsurl = url.replace(scheme="ws").child("private", "logs", "v1")
factory = WebSocketClientFactory(
url=wsurl.to_uri().to_text(),
headers={
"Authorization": "{} {}".format("tahoe-lafs", api_token),
},
)
factory.protocol = TahoeLogReader
factory.streamedlogs = self
endpoint = TCP4ClientEndpoint(self._reactor, url.host, url.port)
client_service = ClientService(endpoint, factory, clock=self._reactor)
return client_service
示例12: connect_to_log_endpoint
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import TCP4ClientEndpoint [as 别名]
def connect_to_log_endpoint(reactor, tahoe, real_reactor, protocolClass):
server_port = fake_log_server(protocolClass)
server_addr = server_port.getHost()
# Make sure the streamed logs websocket client can compute the correct ws
# url. If it doesn't match the server, the handshake fails.
tahoe.nodeurl = "http://{}:{}".format(server_addr.host, server_addr.port)
tahoe.streamedlogs.start(tahoe.nodeurl, tahoe.api_token)
_, _, client_factory = reactor.connectTCP.call_args[0]
endpoint = TCP4ClientEndpoint(
real_reactor,
# Windows doesn't like to connect to 0.0.0.0.
"127.0.0.1" if server_addr.host == "0.0.0.0" else server_addr.host,
server_addr.port,
)
return endpoint.connect(client_factory)
示例13: test_binary_messages_dropped
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import TCP4ClientEndpoint [as 别名]
def test_binary_messages_dropped(reactor, tahoe):
from twisted.internet import reactor as real_reactor
server = BinaryMessageServerProtocol()
client = yield connect_to_log_endpoint(
reactor, tahoe, real_reactor, lambda: server,
)
# client is a _WrappingProtocol because the implementation uses
# TCP4ClientEndpoint which puts a _WrappingFactory into the reactor. Then
# connect_to_log_endpoint takes the _WrappingFactory out of the _mock_
# reactor it supplied and puts it into a new TCP4ClientEndpoint and uses
# that against a real reactor. The connection gets set up and the
# _WrappingFactory creates a _WrappingProtocol which the new
# TCP4ClientEndpoint happily hands back to us.
#
# Sadly, this hack makes us dependent on the implementation of endpoints,
# the use of endpoints in streamedlogs.py, and the use of endpoints in
# connect_to_log_endpoint.
#
# Maybe it would be good to try to use twisted.test.iosim instead? Or
# build something like RequestTraversalAgent but for Autobahn/WebSocket.
yield client._wrappedProtocol.is_closed
assert tahoe.streamedlogs.get_streamed_log_messages() == []
示例14: connect
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import TCP4ClientEndpoint [as 别名]
def connect(self):
log.info('Connecting')
factory = MQTTFactory(profile=MQTTFactory.PUBLISHER | MQTTFactory.SUBSCRIBER)
point = TCP4ClientEndpoint(reactor, self.broker_host, self.broker_port)
d = point.connect(factory).addCallback(self.gotProtocol)
d.addErrback(self.on_error)
示例15: client
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import TCP4ClientEndpoint [as 别名]
def client(self, reactor, serverAddress):
"""
Create a client end point that will connect to the given address.
@type serverAddress: L{IPv4Address}
"""
return TCP4ClientEndpoint(reactor, self.interface, serverAddress.port)