Python twisted.internet.endpoints.connectProtocol() Examples
The following are 14
code examples of twisted.internet.endpoints.connectProtocol().
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: process.py From bitmask-dev with GNU General Public License v3.0 | 6 votes |
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))
Example #2
Source File: test_endpoints.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
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)
Example #3
Source File: endpoints.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
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
Example #4
Source File: zfs.py From flocker with Apache License 2.0 | 6 votes |
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
Example #5
Source File: test_endpoints.py From learn_python3_spider with MIT License | 6 votes |
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)
Example #6
Source File: endpoints.py From learn_python3_spider with MIT License | 6 votes |
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
Example #7
Source File: test_endpoints.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
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()))
Example #8
Source File: forwarding.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
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 #9
Source File: test_endpoints.py From learn_python3_spider with MIT License | 5 votes |
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()))
Example #10
Source File: forwarding.py From learn_python3_spider with MIT License | 5 votes |
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 #11
Source File: test_tensor.py From tensor with MIT License | 5 votes |
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()
Example #12
Source File: clusterservice.py From maas with GNU Affero General Public License v3.0 | 5 votes |
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)
Example #13
Source File: fixtures.py From maas with GNU Affero General Public License v3.0 | 5 votes |
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)
Example #14
Source File: __init__.py From maas with GNU Affero General Public License v3.0 | 4 votes |
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.