本文整理汇总了Python中twisted.internet.endpoints.connectProtocol方法的典型用法代码示例。如果您正苦于以下问题:Python endpoints.connectProtocol方法的具体用法?Python endpoints.connectProtocol怎么用?Python endpoints.connectProtocol使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.endpoints
的用法示例。
在下文中一共展示了endpoints.connectProtocol方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _connect_to_management
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import connectProtocol [as 别名]
def _connect_to_management(self, retries=30):
if retries == 0:
self.log.error('Timeout while connecting to management')
self.failed = True
return
def retry(retries):
ctr = retries - 1
self.log.warn(
'Error connecting to management, retrying. '
'Retries left: %s' % ctr)
reactor.callLater(
0.1, self._connect_to_management, ctr)
self._d = connectProtocol(
self._management_endpoint,
ManagementProtocol(verbose=True))
self._d.addCallbacks(
self._got_management_protocol,
lambda f: retry(retries))
示例2: test_connectProtocolCreatesFactory
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import connectProtocol [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)
示例3: secureConnection
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import connectProtocol [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
示例4: zfs_command
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import connectProtocol [as 别名]
def zfs_command(reactor, arguments):
"""
Asynchronously run the ``zfs`` command-line tool with the given arguments.
:param reactor: A ``IReactorProcess`` provider.
:param arguments: A ``list`` of ``bytes``, command-line arguments to
``zfs``.
:return: A :class:`Deferred` firing with the bytes of the result (on
exit code 0), or errbacking with :class:`CommandFailed` or
:class:`BadArguments` depending on the exit code (1 or 2).
"""
endpoint = ProcessEndpoint(reactor, b"zfs", [b"zfs"] + arguments,
os.environ)
d = connectProtocol(endpoint, _AccumulatingProtocol())
d.addCallback(lambda protocol: protocol._result)
return d
示例5: test_connectProtocolReturnsConnectResult
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import connectProtocol [as 别名]
def test_connectProtocolReturnsConnectResult(self):
"""
C{endpoints.connectProtocol} returns the result of calling the given
endpoint's C{connect()} method.
"""
result = defer.Deferred()
class Endpoint:
def connect(self, factory):
"""
Return a marker object for use in our assertion.
"""
return result
endpoint = Endpoint()
self.assertIs(result, endpoints.connectProtocol(endpoint, object()))
示例6: channelOpen
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import connectProtocol [as 别名]
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
示例7: test_tcp_riemann
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import connectProtocol [as 别名]
def test_tcp_riemann(self):
event = Event('ok', 'sky', 'Sky has not fallen', 1.0, 60.0)
end = TCP4ClientEndpoint(reactor, "localhost", 5555)
p = yield connectProtocol(end, riemann.RiemannProtocol())
yield p.sendEvents([event])
p.transport.loseConnection()
示例8: _make_connection
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import connectProtocol [as 别名]
def _make_connection(self, eventloop, address):
"""Connect to `eventloop` at `address`."""
# Force everything to use AF_INET6 sockets.
endpoint = TCP6ClientEndpoint(self.clock, *address)
protocol = ClusterClient(address, eventloop, self)
return connectProtocol(endpoint, protocol)
示例9: addCluster
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import connectProtocol [as 别名]
def addCluster(self, protocol, rack_controller):
"""Add a new stub cluster using the given `protocol`.
The `protocol` should be an instance of `amp.AMP`.
:return: A `Deferred` that fires with the connected protocol
instance.
"""
endpoint = endpoints.UNIXClientEndpoint(reactor, self.sockfile)
protocol = yield endpoints.connectProtocol(endpoint, protocol)
# Mock the registration into the database, as the rack controller is
# already created. We reset this once registration is complete so as
# to not interfere with other tests.
registered = rack_controller
patcher = MonkeyPatcher()
patcher.add_patch(
rackcontrollers, "register", (lambda *args, **kwargs: registered)
)
# Register the rack controller with the region.
patcher.patch()
try:
yield protocol.callRemote(
region.RegisterRackController,
system_id=rack_controller.system_id,
hostname=rack_controller.hostname,
interfaces={},
url=urlparse(""),
)
finally:
patcher.restore()
defer.returnValue(protocol)
示例10: connect
# 需要导入模块: from twisted.internet import endpoints [as 别名]
# 或者: from twisted.internet.endpoints import connectProtocol [as 别名]
def connect(self, cluster, region):
"""Wire up a connection between cluster and region.
Uses a UNIX socket to very rapidly connect the two ends.
:type cluster: `twisted.internet.interfaces.IProtocol`
:type region: `twisted.internet.interfaces.IProtocol`
"""
# Wire up the region and cluster protocols via the sockfile.
sockfile = path.join(self.sockdir.path, next(self.socknames))
class RegionFactory(Factory):
def buildProtocol(self, addr):
return region
# `doUpdate` has already been called, but with no connections the
# mocked `_fetch_rpc_info` caused no `maas_url` to be set on the
# RPC service. Set the `maas_url` to the one set on the fixture.
self.rpc_service.maas_url = self.maas_url
endpoint_region = endpoints.UNIXServerEndpoint(reactor, sockfile)
port = yield endpoint_region.listen(RegionFactory())
endpoint_cluster = endpoints.UNIXClientEndpoint(reactor, sockfile)
client = yield endpoints.connectProtocol(endpoint_cluster, cluster)
# Wait for the client to be fully connected. Because onReady will have
# been capped-off by now (see ClusterClient.connectionMade) this will
# not raise any exceptions. In some ways this is convenient because it
# allows the resulting issues to be encountered within test code.
yield client.ready.get()
@inlineCallbacks
def shutdown():
# We need to make sure that everything is shutdown correctly. TLS
# seems to make this even more important: it complains loudly if
# connections are not closed cleanly. An interesting article to
# read now is Jono Lange's "How to Disconnect in Twisted, Really"
# <http://mumak.net/stuff/twisted-disconnect.html>.
yield port.loseConnection()
yield port.deferred
if region.transport is not None:
yield region.transport.loseConnection()
yield region.onConnectionLost
if client.transport is not None:
yield client.transport.loseConnection()
yield client.onConnectionLost
# Fixtures don't wait for deferred work in clean-up tasks (or anywhere
# else), so we can't use `self.addCleanup(shutdown)` here. We need to
# get the user to add `shutdown` to the clean-up tasks for the *test*,
# on the assumption they're using a test framework that accommodates
# deferred work (like testtools with `MAASTwistedRunTest`).
returnValue(shutdown)
# An iterable of names for new dynamically-created AMP protocol factories.