Python twisted.internet.endpoints.TCP4ServerEndpoint() Examples
The following are 30
code examples of twisted.internet.endpoints.TCP4ServerEndpoint().
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_txsni.py From txsni with MIT License | 6 votes |
def sni_endpoint(): """ Builds a TxSNI TLSEndpoint populated with the default certificates. These are built from cert_builder.py, and have the following certs in the SNI map: - DEFAULT.pem, which contains a SAN for 'localhost'. - http2bin.org.pem, which contains a SAN for 'http2bin.org' """ base_endpoint = endpoints.TCP4ServerEndpoint( reactor=reactor, port=0, interface='127.0.0.1', ) path = FilePath(CERT_DIR) mapping = SNIMap(HostDirectoryMap(path)) wrapper_endpoint = TLSEndpoint(base_endpoint, mapping) return wrapper_endpoint
Example #2
Source File: vmware_exporter.py From vmware_exporter with BSD 3-Clause "New" or "Revised" License | 6 votes |
def main(): """ start up twisted reactor """ parser = argparse.ArgumentParser(description='VMWare metrics exporter for Prometheus') parser.add_argument('-c', '--config', dest='config_file', default=None, help="configuration file") parser.add_argument('-p', '--port', dest='port', type=int, default=9272, help="HTTP port to expose metrics") args = parser.parse_args() # Start up the server to expose the metrics. root = VMWareMetricsResource() root.configure(args) root.putChild(b'metrics', VMWareMetricsResource()) root.putChild(b'healthz', VMWareMetricsResource()) factory = Site(root) log("Starting web server on port {}".format(args.port)) endpoint = endpoints.TCP4ServerEndpoint(reactor, args.port) endpoint.listen(factory) reactor.run()
Example #3
Source File: test_endpoints.py From python-for-android with Apache License 2.0 | 6 votes |
def createServerEndpoint(self, reactor, factory, **listenArgs): """ Create an L{TCP4ServerEndpoint} and return the values needed to verify its behaviour. @param reactor: A fake L{IReactorTCP} that L{TCP4ServerEndpoint} can call L{IReactorTCP.listenTCP} on. @param factory: The thing that we expect to be passed to our L{IStreamServerEndpoint.listen} implementation. @param listenArgs: Optional dictionary of arguments to L{IReactorTCP.listenTCP}. """ address = IPv4Address("TCP", "0.0.0.0", 0) if listenArgs is None: listenArgs = {} return (endpoints.TCP4ServerEndpoint(reactor, address.port, **listenArgs), (address.port, factory, listenArgs.get('backlog', 50), listenArgs.get('interface', '')), address)
Example #4
Source File: test_strports.py From python-for-android with Apache License 2.0 | 6 votes |
def test_service(self): """ L{strports.service} returns a L{StreamServerEndpointService} constructed with an endpoint produced from L{endpoint.serverFromString}, using the same syntax. """ reactor = object() # the cake is a lie aFactory = Factory() aGoodPort = 1337 svc = strports.service( 'tcp:'+str(aGoodPort), aFactory, reactor=reactor) self.assertIsInstance(svc, internet.StreamServerEndpointService) # See twisted.application.test.test_internet.TestEndpointService. # test_synchronousRaiseRaisesSynchronously self.assertEquals(svc._raiseSynchronously, True) self.assertIsInstance(svc.endpoint, TCP4ServerEndpoint) # Maybe we should implement equality for endpoints. self.assertEquals(svc.endpoint._port, aGoodPort) self.assertIdentical(svc.factory, aFactory) self.assertIdentical(svc.endpoint._reactor, reactor)
Example #5
Source File: iosim.py From learn_python3_spider with MIT License | 6 votes |
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)
Example #6
Source File: test_strports.py From learn_python3_spider with MIT License | 6 votes |
def test_service(self): """ L{strports.service} returns a L{StreamServerEndpointService} constructed with an endpoint produced from L{endpoint.serverFromString}, using the same syntax. """ reactor = object() # the cake is a lie aFactory = Factory() aGoodPort = 1337 svc = strports.service( 'tcp:' + str(aGoodPort), aFactory, reactor=reactor) self.assertIsInstance(svc, internet.StreamServerEndpointService) # See twisted.application.test.test_internet.EndpointServiceTests. # test_synchronousRaiseRaisesSynchronously self.assertTrue(svc._raiseSynchronously) self.assertIsInstance(svc.endpoint, TCP4ServerEndpoint) # Maybe we should implement equality for endpoints. self.assertEqual(svc.endpoint._port, aGoodPort) self.assertIs(svc.factory, aFactory) self.assertIs(svc.endpoint._reactor, reactor)
Example #7
Source File: test_endpoints.py From learn_python3_spider with MIT License | 6 votes |
def createServerEndpoint(self, reactor, factory, **listenArgs): """ Create an L{TCP4ServerEndpoint} and return the values needed to verify its behaviour. @param reactor: A fake L{IReactorTCP} that L{TCP4ServerEndpoint} can call L{IReactorTCP.listenTCP} on. @param factory: The thing that we expect to be passed to our L{IStreamServerEndpoint.listen} implementation. @param listenArgs: Optional dictionary of arguments to L{IReactorTCP.listenTCP}. """ address = IPv4Address("TCP", "0.0.0.0", 0) if listenArgs is None: listenArgs = {} return (endpoints.TCP4ServerEndpoint(reactor, address.port, **listenArgs), (address.port, factory, listenArgs.get('backlog', 50), listenArgs.get('interface', '')), address)
Example #8
Source File: csruntime.py From calvin-base with Apache License 2.0 | 6 votes |
def start_gui(interface4, port, mockdevices): import calvinextras import inspect import os.path from twisted.web.server import Site from twisted.web.static import File from twisted.internet import endpoints, reactor from calvin.utilities import calvinconfig # find installation path of calvinextras package extras_path = os.path.dirname(inspect.getfile(calvinextras)) # build path to gui files gui_path = os.path.join(extras_path, "CalvinGUI", "Build", "GUI") gui_config_path = os.path.join(extras_path, "CalvinGUI", "calvin.conf") if mockdevices: # Patch config _conf = calvinconfig.get() delta_config = _conf.config_at_path(gui_config_path) _conf.update_config(delta_config) # Add endpoint to twisted reactor resource = File(gui_path) factory = Site(resource) endpoint = endpoints.TCP4ServerEndpoint(reactor, interface=interface4, port=port) endpoint.listen(factory) _log.info("Calvin GUI server listening on http://{}:{}".format(interface4, port))
Example #9
Source File: iosim.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
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)
Example #10
Source File: test_strports.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_service(self): """ L{strports.service} returns a L{StreamServerEndpointService} constructed with an endpoint produced from L{endpoint.serverFromString}, using the same syntax. """ reactor = object() # the cake is a lie aFactory = Factory() aGoodPort = 1337 svc = strports.service( 'tcp:' + str(aGoodPort), aFactory, reactor=reactor) self.assertIsInstance(svc, internet.StreamServerEndpointService) # See twisted.application.test.test_internet.EndpointServiceTests. # test_synchronousRaiseRaisesSynchronously self.assertTrue(svc._raiseSynchronously) self.assertIsInstance(svc.endpoint, TCP4ServerEndpoint) # Maybe we should implement equality for endpoints. self.assertEqual(svc.endpoint._port, aGoodPort) self.assertIs(svc.factory, aFactory) self.assertIs(svc.endpoint._reactor, reactor)
Example #11
Source File: test_endpoints.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def createServerEndpoint(self, reactor, factory, **listenArgs): """ Create an L{TCP4ServerEndpoint} and return the values needed to verify its behaviour. @param reactor: A fake L{IReactorTCP} that L{TCP4ServerEndpoint} can call L{IReactorTCP.listenTCP} on. @param factory: The thing that we expect to be passed to our L{IStreamServerEndpoint.listen} implementation. @param listenArgs: Optional dictionary of arguments to L{IReactorTCP.listenTCP}. """ address = IPv4Address("TCP", "0.0.0.0", 0) if listenArgs is None: listenArgs = {} return (endpoints.TCP4ServerEndpoint(reactor, address.port, **listenArgs), (address.port, factory, listenArgs.get('backlog', 50), listenArgs.get('interface', '')), address)
Example #12
Source File: vmware_exporter.py From vmware_exporter with BSD 3-Clause "New" or "Revised" License | 6 votes |
def main(argv=None): """ start up twisted reactor """ parser = argparse.ArgumentParser(description='VMWare metrics exporter for Prometheus') parser.add_argument('-c', '--config', dest='config_file', default=None, help="configuration file") parser.add_argument('-p', '--port', dest='port', type=int, default=9272, help="HTTP port to expose metrics") parser.add_argument('-l', '--loglevel', dest='loglevel', default="INFO", help="Set application loglevel INFO, DEBUG") args = parser.parse_args(argv or sys.argv[1:]) numeric_level = getattr(logging, args.loglevel.upper(), None) if not isinstance(numeric_level, int): raise ValueError("Invalid log level: {level}".format(level=args.loglevel)) logging.basicConfig(level=numeric_level, format='%(asctime)s %(levelname)s:%(message)s') reactor.suggestThreadPoolSize(25) factory = Site(registerEndpoints(args)) logging.info("Starting web server on port {port}".format(port=args.port)) endpoint = endpoints.TCP4ServerEndpoint(reactor, args.port) endpoint.listen(factory) reactor.run()
Example #13
Source File: test_service.py From tensor with MIT License | 5 votes |
def start_riemann_server(self): factory = FakeRiemannServerFactory() self.addCleanup(factory.stop_listening) return factory.start_listening(TCP4ServerEndpoint(reactor, 0))
Example #14
Source File: common.py From magic-wormhole-mailbox-server with MIT License | 5 votes |
def _setup_relay(self, do_listen=False, web_log_requests=False, **kwargs): channel_db = create_or_upgrade_channel_db(":memory:") self._server = make_server(channel_db, **kwargs) if do_listen: ep = endpoints.TCP4ServerEndpoint(reactor, 0, interface="127.0.0.1") self._site = make_web_server(self._server, log_requests=web_log_requests) self._lp = yield ep.listen(self._site) addr = self._lp.getHost() self.relayurl = "ws://127.0.0.1:%d/v1" % addr.port self.rdv_ws_port = addr.port
Example #15
Source File: test_regionservice.py From maas with GNU Affero General Public License v3.0 | 5 votes |
def test_start_up_binds_first_of_real_endpoint_options(self): service = RegionService(sentinel.ipcWorker) # endpoint_1.listen(...) will bind to a random high-numbered port. endpoint_1 = TCP4ServerEndpoint(reactor, 0) # endpoint_2.listen(...), if attempted, will crash because only root # (or a user with explicit capabilities) can do stuff like that. It's # a reasonable assumption that the user running these tests is not # root, but we'll check the port number later too to be sure. endpoint_2 = TCP4ServerEndpoint(reactor, 1) service.endpoints = [[endpoint_1, endpoint_2]] yield service.startService() self.addCleanup(wait_for_reactor(service.stopService)) # A single port has been bound. self.assertThat( service.ports, MatchesAll(HasLength(1), AllMatch(IsInstance(tcp.Port))), ) # The port is not listening on port 1; i.e. a belt-n-braces check that # endpoint_2 was not used. [port] = service.ports self.assertThat(port.getHost().port, Not(Equals(1)))
Example #16
Source File: test_strports.py From python-for-android with Apache License 2.0 | 5 votes |
def test_serviceDeprecatedUnqualified(self): """ Unqualified strport descriptions, i.e. "8080", are deprecated. """ svc = strports.service("8080", None) self.assertIsInstance(svc.endpoint, TCP4ServerEndpoint) warnings = self.flushWarnings( [self.test_serviceDeprecatedUnqualified]) self.assertEquals(warnings[0]['category'], DeprecationWarning) self.assertEquals( warnings[0]['message'], "Unqualified strport description passed to 'service'." "Use qualified endpoint descriptions; for example, 'tcp:8080'.") self.assertEquals(len(warnings), 1)
Example #17
Source File: test_webapp.py From maas with GNU Affero General Public License v3.0 | 5 votes |
def make_webapp(self): listener = FakePostgresListenerService() service = webapp.WebApplicationService( 0, listener, sentinel.status_worker ) mock_makeEndpoint = self.patch(service, "_makeEndpoint") mock_makeEndpoint.return_value = TCP4ServerEndpoint( reactor, 0, interface="localhost" ) return service
Example #18
Source File: simulated_olt.py From voltha with Apache License 2.0 | 5 votes |
def start(self): log.debug('starting') # setup a basic web server for test control self.control_endpoint = endpoints.TCP4ServerEndpoint(reactor, 18880) self.control_endpoint.listen(self.get_test_control_site()) log.info('started')
Example #19
Source File: test_strports.py From python-for-android with Apache License 2.0 | 5 votes |
def test_serviceDeprecatedDefault(self): """ L{strports.service} still accepts a 'default' argument, which will affect the parsing of 'default' (i.e. 'not containing a colon') endpoint descriptions, but this behavior is deprecated. """ svc = strports.service("8080", None, "unix") self.assertIsInstance(svc.endpoint, UNIXServerEndpoint) warnings = self.flushWarnings([self.test_serviceDeprecatedDefault]) self.assertEquals(warnings[0]['category'], DeprecationWarning) self.assertEquals( warnings[0]['message'], "The 'default' parameter was deprecated in Twisted 10.2.0. " "Use qualified endpoint descriptions; for example, 'tcp:8080'.") self.assertEquals(len(warnings), 1) # Almost the same case, but slightly tricky - explicitly passing the old # default value, None, also must trigger a deprecation warning. svc = strports.service("tcp:8080", None, None) self.assertIsInstance(svc.endpoint, TCP4ServerEndpoint) warnings = self.flushWarnings([self.test_serviceDeprecatedDefault]) self.assertEquals(warnings[0]['category'], DeprecationWarning) self.assertEquals( warnings[0]['message'], "The 'default' parameter was deprecated in Twisted 10.2.0.") self.assertEquals(len(warnings), 1)
Example #20
Source File: test_endpoints.py From python-for-android with Apache License 2.0 | 5 votes |
def test_tcp(self): """ When passed a TCP strports description, L{endpoints.serverFromString} returns a L{TCP4ServerEndpoint} instance initialized with the values from the string. """ reactor = object() server = endpoints.serverFromString( reactor, "tcp:1234:backlog=12:interface=10.0.0.1") self.assertIsInstance(server, endpoints.TCP4ServerEndpoint) self.assertIdentical(server._reactor, reactor) self.assertEquals(server._port, 1234) self.assertEquals(server._backlog, 12) self.assertEquals(server._interface, "10.0.0.1")
Example #21
Source File: health_check.py From voltha with Apache License 2.0 | 5 votes |
def init_rest_service(port): hc = HealthCheck() endpoint = endpoints.TCP4ServerEndpoint(reactor, port) endpoint.listen(hc.get_site())
Example #22
Source File: common.py From magic-wormhole with MIT License | 5 votes |
def _setup_relay(self, error, advertise_version=None): self.sp = service.MultiService() self.sp.startService() # need to talk to twisted team about only using unicode in # endpoints.serverFromString db = create_channel_db(":memory:") self._usage_db = create_usage_db(":memory:") self._rendezvous = make_server( db, advertise_version=advertise_version, signal_error=error, usage_db=self._usage_db) ep = endpoints.TCP4ServerEndpoint(reactor, 0, interface="127.0.0.1") site = make_web_server(self._rendezvous, log_requests=False) # self._lp = yield ep.listen(site) s = MyInternetService(ep, site) s.setServiceParent(self.sp) self.rdv_ws_port = yield s.getPort() self._relay_server = s # self._rendezvous = s._rendezvous self.relayurl = u"ws://127.0.0.1:%d/v1" % self.rdv_ws_port # ws://127.0.0.1:%d/wormhole-relay/ws self.transitport = allocate_tcp_port() ep = endpoints.serverFromString( reactor, "tcp:%d:interface=127.0.0.1" % self.transitport) self._transit_server = f = Transit( blur_usage=None, log_file=None, usage_db=None) internet.StreamServerEndpointService(ep, f).setServiceParent(self.sp) self.transit = u"tcp:127.0.0.1:%d" % self.transitport
Example #23
Source File: test_transit.py From magic-wormhole with MIT License | 5 votes |
def test_listener(self): c = transit.Common("") hints, ep = c._build_listener() self.assertIsInstance(hints, (list, set)) if hints: self.assertIsInstance(hints[0], DirectTCPV1Hint) self.assertIsInstance(ep, endpoints.TCP4ServerEndpoint)
Example #24
Source File: test_sources.py From tensor with MIT License | 5 votes |
def start_fake_riak_server(self, stats): def cb(listener): self.addCleanup(listener.stopListening) return listener data = static.Data(json.dumps(stats).encode(), 'application/json') data.isLeaf = True site = server.Site(data) endpoint = endpoints.TCP4ServerEndpoint(reactor, 0) return endpoint.listen(site).addCallback(cb)
Example #25
Source File: test_endpoints.py From learn_python3_spider with MIT License | 5 votes |
def test_tcp(self): """ When passed a TCP strports description, L{endpoints.serverFromString} returns a L{TCP4ServerEndpoint} instance initialized with the values from the string. """ reactor = object() server = endpoints.serverFromString( reactor, "tcp:1234:backlog=12:interface=10.0.0.1") self.assertIsInstance(server, endpoints.TCP4ServerEndpoint) self.assertIs(server._reactor, reactor) self.assertEqual(server._port, 1234) self.assertEqual(server._backlog, 12) self.assertEqual(server._interface, "10.0.0.1")
Example #26
Source File: test_tcp.py From learn_python3_spider with MIT License | 5 votes |
def server(self, reactor): """ Create a server-side TCP endpoint. """ return TCP4ServerEndpoint(reactor, 0, interface=self.interface)
Example #27
Source File: queue.py From ccs-twistedextensions with Apache License 2.0 | 5 votes |
def startService(self): """ Register ourselves with the database and establish all outgoing connections to other servers in the cluster. """ @inlineCallbacks def startup(txn): endpoint = TCP4ServerEndpoint(self.reactor, self.ampPort) # If this fails, the failure mode is going to be ugly, just like # all conflicted-port failures. But, at least it won't proceed. self._listeningPort = yield endpoint.listen(self.peerFactory()) self.ampPort = self._listeningPort.getHost().port yield Lock.exclusive(NodeInfo.table).on(txn) nodes = yield self.activeNodes(txn) selves = [node for node in nodes if ((node.hostname == self.hostname) and (node.port == self.ampPort))] if selves: self.thisProcess = selves[0] nodes.remove(self.thisProcess) yield self.thisProcess.update(pid=self.pid, time=datetime.now()) else: self.thisProcess = yield NodeInfo.create( txn, hostname=self.hostname, port=self.ampPort, pid=self.pid, time=datetime.now() ) for node in nodes: self._startConnectingTo(node) self._startingUp = inTransaction(self.transactionFactory, startup) @self._startingUp.addBoth def done(result): self._startingUp = None super(PeerConnectionPool, self).startService() self._lostWorkCheckLoop() return result
Example #28
Source File: test_httpapi.py From flocker with Apache License 2.0 | 5 votes |
def test_returns_service(self): """ ``create_api_service`` returns an object providing ``IService``. """ reactor = MemoryReactor() endpoint = TCP4ServerEndpoint(reactor, 6789) verifyObject(IService, create_api_service( ConfigurationPersistenceService(reactor, FilePath(self.mktemp())), ClusterStateService(reactor), endpoint, ClientContextFactory()))
Example #29
Source File: test_options.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def _endpointTest(self, service): """ Use L{Options} to parse a single service configuration parameter and verify that an endpoint of the correct type is added to the list for that service. """ options = Options() options.parseOptions(['--' + service, 'tcp:1234']) self.assertEqual(len(options[service]), 1) self.assertIsInstance( options[service][0], endpoints.TCP4ServerEndpoint)
Example #30
Source File: tap.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def _getEndpoints(self, reactor, service): """ Return a list of endpoints for the specified service, constructing defaults if necessary. If no endpoints were configured for the service and the protocol was not explicitly disabled with a I{--no-*} option, a default endpoint for the service is created. @type reactor: L{IReactorTCP <twisted.internet.interfaces.IReactorTCP>} provider @param reactor: If any endpoints are created, the reactor with which they are created. @type service: L{bytes} @param service: The type of service for which to retrieve endpoints, either C{b'pop3'} or C{b'smtp'}. @rtype: L{list} of L{IStreamServerEndpoint <twisted.internet.interfaces.IStreamServerEndpoint>} provider @return: The endpoints for the specified service as configured by the command line parameters. """ if self[service]: # If there are any services set up, just return those. return self[service] elif self['no-' + service]: # If there are no services, but the service was explicitly disabled, # return nothing. return [] else: # Otherwise, return the old default service. return [ endpoints.TCP4ServerEndpoint( reactor, self._protoDefaults[service])]