Python twisted.application.internet.StreamServerEndpointService() Examples

The following are 27 code examples of twisted.application.internet.StreamServerEndpointService(). 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.application.internet , or try the search function .
Example #1
Source File: strports.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def service(description, factory, reactor=None):
    """
    Return the service corresponding to a description.

    @param description: The description of the listening port, in the syntax
        described by L{twisted.internet.endpoints.serverFromString}.
    @type description: C{str}

    @param factory: The protocol factory which will build protocols for
        connections to this service.
    @type factory: L{twisted.internet.interfaces.IProtocolFactory}

    @rtype: C{twisted.application.service.IService}
    @return: the service corresponding to a description of a reliable stream
        server.

    @see: L{twisted.internet.endpoints.serverFromString}
    """
    if reactor is None:
        from twisted.internet import reactor

    svc = StreamServerEndpointService(
        endpoints.serverFromString(reactor, description), factory)
    svc._raiseSynchronously = True
    return svc 
Example #2
Source File: test_strports.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
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 #3
Source File: test_manhole_tap.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_sshPort(self):
        """
        L{manhole_tap.makeService} will make a SSH service on the port
        defined by C{--sshPort}. It will not make a telnet service.
        """
        # Why the sshKeyDir and sshKeySize params? To prevent it stomping over
        # (or using!) the user's private key, we just make a super small one
        # which will never be used in a temp directory.
        self.options.parseOptions(["--sshKeyDir", self.mktemp(),
                                   "--sshKeySize", "512",
                                   "--sshPort", "tcp:223"])
        service = manhole_tap.makeService(self.options)
        self.assertIsInstance(service, MultiService)
        self.assertEqual(len(service.services), 1)
        self.assertIsInstance(service.services[0], StreamServerEndpointService)
        self.assertIsInstance(service.services[0].factory,
                              manhole_ssh.ConchFactory)
        self.assertEqual(service.services[0].endpoint._port, 223) 
Example #4
Source File: strports.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def service(description, factory, reactor=None):
    """
    Return the service corresponding to a description.

    @param description: The description of the listening port, in the syntax
        described by L{twisted.internet.endpoints.serverFromString}.
    @type description: C{str}

    @param factory: The protocol factory which will build protocols for
        connections to this service.
    @type factory: L{twisted.internet.interfaces.IProtocolFactory}

    @rtype: C{twisted.application.service.IService}
    @return: the service corresponding to a description of a reliable stream
        server.

    @see: L{twisted.internet.endpoints.serverFromString}
    """
    if reactor is None:
        from twisted.internet import reactor

    svc = StreamServerEndpointService(
        endpoints.serverFromString(reactor, description), factory)
    svc._raiseSynchronously = True
    return svc 
Example #5
Source File: test_strports.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
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 #6
Source File: test_manhole_tap.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_sshPort(self):
        """
        L{manhole_tap.makeService} will make a SSH service on the port
        defined by C{--sshPort}. It will not make a telnet service.
        """
        # Why the sshKeyDir and sshKeySize params? To prevent it stomping over
        # (or using!) the user's private key, we just make a super small one
        # which will never be used in a temp directory.
        self.options.parseOptions(["--sshKeyDir", self.mktemp(),
                                   "--sshKeySize", "512",
                                   "--sshPort", "tcp:223"])
        service = manhole_tap.makeService(self.options)
        self.assertIsInstance(service, MultiService)
        self.assertEqual(len(service.services), 1)
        self.assertIsInstance(service.services[0], StreamServerEndpointService)
        self.assertIsInstance(service.services[0].factory,
                              manhole_ssh.ConchFactory)
        self.assertEqual(service.services[0].endpoint._port, 223) 
Example #7
Source File: demo-journal.py    From magic-wormhole with MIT License 5 votes vote down vote up
def __init__(self, basedir, reactor):
        service.MultiService.__init__(self)
        self._basedir = basedir
        self._reactor = reactor

        root = Root()
        site = server.Site(root)
        ep = endpoints.serverFromString(reactor, "tcp:8220")
        internet.StreamServerEndpointService(ep, site).setServiceParent(self)

        self._jm = journal.JournalManager(self._save_state)

        root.putChild(b"", static.Data("root", "text/plain"))
        root.putChild(b"list-invitations", Status(self._list_invitations))
        root.putChild(b"invite", Action(self._invite))  # {petname:}
        root.putChild(b"accept", Action(self._accept))  # {petname:, code:}

        self._state_fn = os.path.join(self._basedir, "state.json")
        self._state = State.from_filename(self._state_fn)

        self._wormholes = {}
        for iid, invitation_state in self._state.get_all_invitations().items():
            def _dispatch(event, *args, **kwargs):
                self._dispatch_wormhole_event(iid, event, *args, **kwargs)
            w = wormhole.journaled_from_data(invitation_state["wormhole"],
                                             reactor=self._reactor,
                                             journal=self._jm,
                                             event_handler=self,
                                             event_handler_args=(iid,))
            self._wormholes[iid] = w
            w.setServiceParent(self) 
Example #8
Source File: hpe_plugin_service.py    From python-hpedockerplugin with Apache License 2.0 5 votes vote down vote up
def setupservice(self):
        # TODO: Remove this line when Python Klein pull request #103 is
        # released
        # NOTE: Docker 1.9 will fail without this line. Docker 1.10 will
        # fail as it no longer includes the Host as part of the http header.
        # Therefore, we need to remove this line altogether.
        # 4/6/16 Removing this line as it's causing problems for testers on
        # Docker 1.10. If you're running 1.9, you can apply the Klein fix
        # here https://github.com/twisted/klein.git to fix.
        UNIXAddress.port = 0
        UNIXAddress.host = b"127.0.0.1"

        # Turnoff use of parameterized hpe.conf and use bind mounted
        # configuration file
        # CONFIG = ['--config-file', self._config_file]
        CONFIG = ['--config-file', CONFIG_FILE]

        # Setup the default, hpe3parconfig, and hpelefthandconfig
        # configuration objects.
        try:
            hpedefaultconfig = getdefaultconfig(CONFIG)
        except Exception as ex:
            msg = (_('hpe3pardocker setupservice failed, error is: %s'),
                   six.text_type(ex))
            LOG.error(msg)
            raise exception.HPEPluginStartPluginException(reason=msg)

        # Set Logging level
        logging_level = hpedefaultconfig.logging
        setup_logging('hpe_storage_api', logging_level)

        self._create_listening_directory(PLUGIN_PATH.parent())
        endpoint = serverFromString(self._reactor, "unix:{}:mode=600".
                                    format(PLUGIN_PATH.path))
        servicename = StreamServerEndpointService(endpoint, Site(
            VolumePlugin(self._reactor, hpedefaultconfig).app.resource()))
        return servicename 
Example #9
Source File: service.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def create_prometheus_exporter_service(reactor, port):
    """Return a service exposing prometheus metrics on the specified port."""
    root = Resource()
    root.putChild(b"metrics", PrometheusMetricsResource(PROMETHEUS_METRICS))
    site = Site(root, logFormatter=reducedWebLogFormatter)
    endpoint = TCP6ServerEndpoint(reactor, port)
    service = StreamServerEndpointService(endpoint, site)
    service.setName("prometheus-exporter")
    return service 
Example #10
Source File: test_eventloop.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_make_PrometheusExporterService(self):
        service = eventloop.make_PrometheusExporterService()
        self.assertIsInstance(service, StreamServerEndpointService)
        self.assertEqual(
            service.endpoint._port, eventloop.DEFAULT_PROMETHEUS_EXPORTER_PORT
        )
        # It is registered as a factory in RegionEventLoop.
        self.assertIs(
            eventloop.make_PrometheusExporterService,
            eventloop.loop.factories["prometheus-exporter"]["factory"],
        )
        self.assertTrue(
            eventloop.loop.factories["prometheus-exporter"]["only_on_master"]
        ) 
Example #11
Source File: test_plugin.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_http_service(self):
        options = Options()
        service_maker = ProvisioningServiceMaker("Harry", "Hill")
        service = service_maker.makeService(options, clock=None)
        http_service = service.getServiceNamed("http_service")
        self.assertIsInstance(http_service, StreamServerEndpointService) 
Example #12
Source File: plugin.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def _makeHTTPService(self):
        """Create the HTTP service."""
        from provisioningserver.rackdservices.http import HTTPResource
        from twisted.application.internet import StreamServerEndpointService
        from twisted.internet.endpoints import AdoptedStreamServerEndpoint
        from provisioningserver.utils.twisted import SiteNoLog

        port = 5249
        s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        try:
            s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
        except socket_error as e:
            if e.errno != ENOPROTOOPT:
                raise e
        s.bind(("::", port))
        # Use a backlog of 50, which seems to be fairly common.
        s.listen(50)
        # Adopt this socket into Twisted's reactor.
        site_endpoint = AdoptedStreamServerEndpoint(
            reactor, s.fileno(), s.family
        )
        site_endpoint.port = port  # Make it easy to get the port number.
        site_endpoint.socket = s  # Prevent garbage collection.

        http_service = StreamServerEndpointService(
            site_endpoint, SiteNoLog(HTTPResource())
        )
        http_service.setName("http_service")
        return http_service 
Example #13
Source File: test_xmpproutertap.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_makeService(self):
        """
        The service gets set up with a router and factory.
        """
        opt = tap.Options()
        opt.parseOptions([])
        s = tap.makeService(opt)
        self.assertIsInstance(s, internet.StreamServerEndpointService)
        self.assertEqual('127.0.0.1', s.endpoint._interface)
        self.assertEqual(5347, s.endpoint._port)
        factory = s.factory
        self.assertIsInstance(factory, component.XMPPComponentServerFactory)
        self.assertIsInstance(factory.router, component.Router)
        self.assertEqual('secret', factory.secret)
        self.assertFalse(factory.logTraffic) 
Example #14
Source File: test_tap.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def test_basic(self):
        """
        L{tap.makeService} returns a L{StreamServerEndpointService} instance
        running on TCP port 22, and the linked protocol factory is an instance
        of L{OpenSSHFactory}.
        """
        config = tap.Options()
        service = tap.makeService(config)
        self.assertIsInstance(service, StreamServerEndpointService)
        self.assertEquals(service.endpoint._port, 22)
        self.assertIsInstance(service.factory, OpenSSHFactory) 
Example #15
Source File: test_xmpproutertap.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def test_makeService(self):
        """
        The service gets set up with a router and factory.
        """
        opt = tap.Options()
        opt.parseOptions([])
        s = tap.makeService(opt)
        self.assertIsInstance(s, internet.StreamServerEndpointService)
        self.assertEquals('127.0.0.1', s.endpoint._interface)
        self.assertEquals(5347, s.endpoint._port)
        factory = s.factory
        self.assertIsInstance(factory, component.XMPPComponentServerFactory)
        self.assertIsInstance(factory.router, component.Router)
        self.assertEquals('secret', factory.secret)
        self.assertFalse(factory.logTraffic) 
Example #16
Source File: common.py    From magic-wormhole with MIT License 5 votes vote down vote up
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 #17
Source File: test_tap.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_basic(self):
        """
        L{tap.makeService} returns a L{StreamServerEndpointService} instance
        running on TCP port 22, and the linked protocol factory is an instance
        of L{OpenSSHFactory}.
        """
        config = tap.Options()
        service = tap.makeService(config)
        self.assertIsInstance(service, StreamServerEndpointService)
        self.assertEqual(service.endpoint._port, 22)
        self.assertIsInstance(service.factory, OpenSSHFactory) 
Example #18
Source File: test_manhole_tap.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_telnetPort(self):
        """
        L{manhole_tap.makeService} will make a telnet service on the port
        defined by C{--telnetPort}. It will not make a SSH service.
        """
        self.options.parseOptions(["--telnetPort", "tcp:222"])
        service = manhole_tap.makeService(self.options)
        self.assertIsInstance(service, MultiService)
        self.assertEqual(len(service.services), 1)
        self.assertIsInstance(service.services[0], StreamServerEndpointService)
        self.assertIsInstance(service.services[0].factory.protocol,
                              manhole_tap.makeTelnetProtocol)
        self.assertEqual(service.services[0].endpoint._port, 222) 
Example #19
Source File: test_manhole_tap.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_telnetPort(self):
        """
        L{manhole_tap.makeService} will make a telnet service on the port
        defined by C{--telnetPort}. It will not make a SSH service.
        """
        self.options.parseOptions(["--telnetPort", "tcp:222"])
        service = manhole_tap.makeService(self.options)
        self.assertIsInstance(service, MultiService)
        self.assertEqual(len(service.services), 1)
        self.assertIsInstance(service.services[0], StreamServerEndpointService)
        self.assertIsInstance(service.services[0].factory.protocol,
                              manhole_tap.makeTelnetProtocol)
        self.assertEqual(service.services[0].endpoint._port, 222) 
Example #20
Source File: test_xmpproutertap.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_makeService(self):
        """
        The service gets set up with a router and factory.
        """
        opt = tap.Options()
        opt.parseOptions([])
        s = tap.makeService(opt)
        self.assertIsInstance(s, internet.StreamServerEndpointService)
        self.assertEqual('127.0.0.1', s.endpoint._interface)
        self.assertEqual(5347, s.endpoint._port)
        factory = s.factory
        self.assertIsInstance(factory, component.XMPPComponentServerFactory)
        self.assertIsInstance(factory.router, component.Router)
        self.assertEqual('secret', factory.secret)
        self.assertFalse(factory.logTraffic) 
Example #21
Source File: _script.py    From flocker with Apache License 2.0 5 votes vote down vote up
def main(self, reactor, options):
        # We can use /etc/flocker/agent.yml and /etc/flocker/node.crt to load
        # some information we need:
        agent_config = get_configuration(options)
        control_host = agent_config['control-service']['hostname']

        certificates_path = options["agent-config"].parent()
        control_port = options["rest-api-port"]
        flocker_client = FlockerClient(reactor, control_host, control_port,
                                       certificates_path.child(b"cluster.crt"),
                                       certificates_path.child(b"plugin.crt"),
                                       certificates_path.child(b"plugin.key"))

        self._create_listening_directory(PLUGIN_PATH.parent())

        # Get the node UUID, and then start up:
        # Retry on  *all* errors.
        getting_id = retry_failure(
            reactor=reactor,
            function=flocker_client.this_node_uuid,
            steps=backoff()
        )

        def run_service(node_id):
            endpoint = serverFromString(
                reactor, "unix:{}:mode=600".format(PLUGIN_PATH.path))
            service = StreamServerEndpointService(endpoint, Site(
                VolumePlugin(reactor, flocker_client, node_id).app.resource()))
            return main_for_service(reactor, service)
        getting_id.addCallback(run_service)
        return getting_id 
Example #22
Source File: test_protocol.py    From flocker with Apache License 2.0 5 votes vote down vote up
def test_start_service(self):
        """
        Starting the service listens with a factory that creates
        ``ControlAMP`` instances pointing at the service.
        """
        service = build_control_amp_service(self)
        initial = service.endpoint_service.running
        service.startService()
        control_factory = service.endpoint_service.factory.wrappedFactory
        protocol = control_factory.buildProtocol(None)
        self.assertEqual(
            (initial, service.endpoint_service.running,
             service.endpoint_service.__class__,
             protocol.__class__, protocol.control_amp_service),
            (False, True, StreamServerEndpointService, ControlAMP, service)) 
Example #23
Source File: httpapi.py    From flocker with Apache License 2.0 5 votes vote down vote up
def create_api_service(persistence_service, cluster_state_service, endpoint,
                       context_factory, clock=reactor):
    """
    Create a Twisted Service that serves the API on the given endpoint.

    :param ConfigurationPersistenceService persistence_service: Service
        for retrieving and setting desired configuration.

    :param ClusterStateService cluster_state_service: Service that
        knows about the current state of the cluster.

    :param endpoint: Twisted endpoint to listen on.

    :param context_factory: TLS context factory.

    :param IReactorTime clock: The clock to use for time. By default
        global reactor.

    :return: Service that will listen on the endpoint using HTTP API server.
    """
    api_root = Resource()
    user = ConfigurationAPIUserV1(persistence_service, cluster_state_service,
                                  clock)
    api_root.putChild('v1', user.app.resource())
    api_root._v1_user = user  # For unit testing purposes, alas

    return StreamServerEndpointService(
        endpoint,
        TLSMemoryBIOFactory(
            context_factory,
            False,
            Site(api_root)
        )
    ) 
Example #24
Source File: _protocol.py    From flocker with Apache License 2.0 5 votes vote down vote up
def __init__(self, reactor, cluster_state, configuration_service, endpoint,
                 context_factory):
        """
        :param reactor: See ``ControlServiceLocator.__init__``.
        :param ClusterStateService cluster_state: Object that records known
            cluster state.
        :param ConfigurationPersistenceService configuration_service:
            Persistence service for desired cluster configuration.
        :param endpoint: Endpoint to listen on.
        :param context_factory: TLS context factory.
        """
        self._connections = set()
        self._reactor = reactor
        self._connections_pending_update = set()
        self._current_pending_update_delayed_call = None
        self._current_command = {}
        self._last_received_generation = defaultdict(
            lambda: _ConfigAndStateGeneration()
        )
        self._configuration_generation_tracker = GenerationTracker(100)
        self._state_generation_tracker = GenerationTracker(100)
        self.cluster_state = cluster_state
        self.configuration_service = configuration_service
        self.endpoint_service = StreamServerEndpointService(
            endpoint,
            TLSMemoryBIOFactory(
                context_factory,
                False,
                ServerFactory.forProtocol(lambda: ControlAMP(reactor, self))
            )
        )
        # When configuration changes, notify all connected clients:
        self.configuration_service.register(self._schedule_broadcast_update) 
Example #25
Source File: agent.py    From ccs-calendarserver with Apache License 2.0 4 votes vote down vote up
def makeAgentService(store):
    """
    Returns a service which will process GatewayAMPCommands, using a socket
    file descripter acquired by launchd

    @param store: an already opened store
    @returns: service
    """
    from twisted.internet import reactor

    sockets = launchActivateSocket("AgentSocket")
    fd = sockets[0]

    family = socket.AF_INET
    endpoint = AdoptedStreamServerEndpoint(reactor, fd, family)

    directory = store.directoryService()

    def becameInactive():
        log.warn("Agent inactive; shutting down")
        reactor.stop()

    from twistedcaldav.config import config
    inactivityDetector = InactivityDetector(
        reactor, config.AgentInactivityTimeoutSeconds, becameInactive
    )
    root = Resource()
    root.putChild(
        "gateway",
        AgentGatewayResource(
            store, directory, inactivityDetector
        )
    )

    # We need this service to be able to return com.apple.calendarserver,
    # so tell it not to suppress system accounts.
    directory = OpenDirectoryDirectoryService(
        "/Local/Default", suppressSystemRecords=False
    )

    portal = Portal(
        AgentRealm(root, [u"com.apple.calendarserver"]),
        [HTTPDigestCredentialChecker(directory)]
    )
    credentialFactory = NoQOPDigestCredentialFactory(
        "md5", "/Local/Default"
    )
    wrapper = HTTPAuthSessionWrapper(portal, [credentialFactory])

    site = Site(wrapper)

    return StreamServerEndpointService(endpoint, site) 
Example #26
Source File: tap.py    From Safejumper-for-Desktop with GNU General Public License v2.0 4 votes vote down vote up
def makeService(config):
    """
    Configure a service for operating a mail server.

    The returned service may include POP3 servers, SMTP servers, or both,
    depending on the configuration passed in.  If there are multiple servers,
    they will share all of their non-network state (i.e. the same user accounts
    are available on all of them).

    @type config: L{Options <usage.Options>}
    @param config: Configuration options specifying which servers to include in
        the returned service and where they should keep mail data.

    @rtype: L{IService <twisted.application.service.IService>} provider
    @return: A service which contains the requested mail servers.
    """
    if config['esmtp']:
        rmType = relaymanager.SmartHostESMTPRelayingManager
        smtpFactory = config.service.getESMTPFactory
    else:
        rmType = relaymanager.SmartHostSMTPRelayingManager
        smtpFactory = config.service.getSMTPFactory

    if config['relay']:
        dir = config['relay']
        if not os.path.isdir(dir):
            os.mkdir(dir)

        config.service.setQueue(relaymanager.Queue(dir))
        default = relay.DomainQueuer(config.service)

        manager = rmType(config.service.queue)
        if config['esmtp']:
            manager.fArgs += (None, None)
        manager.fArgs += (config['hostname'],)

        helper = relaymanager.RelayStateHelper(manager, 1)
        helper.setServiceParent(config.service)
        config.service.domains.setDefaultDomain(default)

    if config['pop3']:
        f = config.service.getPOP3Factory()
        for endpoint in config['pop3']:
            svc = internet.StreamServerEndpointService(endpoint, f)
            svc.setServiceParent(config.service)

    if config['smtp']:
        f = smtpFactory()
        if config['hostname']:
            f.domain = config['hostname']
            f.fArgs = (f.domain,)
        if config['esmtp']:
            f.fArgs = (None, None) + f.fArgs
        for endpoint in config['smtp']:
            svc = internet.StreamServerEndpointService(endpoint, f)
            svc.setServiceParent(config.service)

    return config.service 
Example #27
Source File: strports.py    From python-for-android with Apache License 2.0 4 votes vote down vote up
def service(description, factory, default=_DEFAULT, reactor=None):
    """
    Return the service corresponding to a description.

    @param description: The description of the listening port, in the syntax
        described by L{twisted.internet.endpoints.server}.

    @type description: C{str}

    @param factory: The protocol factory which will build protocols for
        connections to this service.

    @type factory: L{twisted.internet.interfaces.IProtocolFactory}

    @type default: C{str} or C{None}

    @param default: Do not use this parameter.  It is deprecated since Twisted
        10.2.0.

    @rtype: C{twisted.application.service.IService}

    @return: the service corresponding to a description of a reliable
        stream server.

    @see: L{twisted.internet.endpoints.serverFromString}
    """
    if reactor is None:
        from twisted.internet import reactor
    if default is _DEFAULT:
        default = None
    else:
        message = "The 'default' parameter was deprecated in Twisted 10.2.0."
        if default is not None:
            message += (
                "  Use qualified endpoint descriptions; for example, "
                "'tcp:%s'." % (description,))
        warnings.warn(
            message=message, category=DeprecationWarning, stacklevel=2)
    svc = StreamServerEndpointService(
        endpoints._serverFromStringLegacy(reactor, description, default),
        factory)
    svc._raiseSynchronously = True
    return svc