twisted.application.service.MultiService

Here are the examples of the python api twisted.application.service.MultiService taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

17 Examples 7

3 Source : tap.py
with MIT License
from autofelix

def makeService(config):
    ca, cl = _buildResolvers(config)

    f = server.DNSServerFactory(config.zones, ca, cl, config['verbose'])
    p = dns.DNSDatagramProtocol(f)
    f.noisy = 0
    ret = service.MultiService()
    for (klass, arg) in [(internet.TCPServer, f), (internet.UDPServer, p)]:
        s = klass(config['port'], arg, interface=config['interface'])
        s.setServiceParent(ret)
    for svc in config.svcs:
        svc.setServiceParent(ret)
    return ret

3 Source : test_application.py
with MIT License
from autofelix

    def testAddingIntoRunning(self):
        p = service.MultiService()
        p.startService()
        s = service.Service()
        self.assertFalse(s.running)
        s.setServiceParent(p)
        self.assertTrue(s.running)
        s.disownServiceParent()
        self.assertFalse(s.running)

    def testPrivileged(self):

3 Source : test_application.py
with MIT License
from autofelix

    def testMultiService(self):
        self.assertTrue(service.IService.providedBy(service.MultiService()))
        self.assertTrue(service.IServiceCollection.providedBy(
                        service.MultiService()))

    def testProcess(self):

3 Source : test_component.py
with MIT License
from fbla-competitive-events

        def runOneTest(self, components):
            self.deferred = defer.Deferred()
            app = service.MultiService()
            for component in components:
                c = wamp.Service(
                    url=self.url,
                    extra=dict(test=self),
                    realm=self.realm,
                    make=component,
                )
                c.setServiceParent(app)

            app.startService()
            yield self.deferred
            app.stopService()

        @defer.inlineCallbacks

3 Source : ssh_tarpit_plugin.py
with MIT License
from westfeld

    def makeService(self, options):
        """
        Construct a TCPServer from a factory defined in ssh_tarpit
        """
        multi_service = MultiService()
        ssh_tarpit_factory = SSHTarpitFactory()
        internet.TCPServer(int(options["port"]), ssh_tarpit_factory,
                           interface=options["bind"]).setServiceParent(multi_service)
        site = server.Site(SSHTarpitStatisticsResource(ssh_tarpit_factory))
        internet.TCPServer(int(options["http_port"]), site,
                           interface=options['http_bind']).setServiceParent(multi_service)
        return multi_service


# Now construct an object which *provides* the relevant interfaces
# The name of this variable is irrelevant, as long as there is *some*
# name bound to a provider of IPlugin and IServiceMaker.

serviceMaker = SSHTarpitServiceMaker()

0 Source : manhole_tap.py
with MIT License
from autofelix

def makeService(options):
    """
    Create a manhole server service.

    @type options: L{dict}
    @param options: A mapping describing the configuration of
    the desired service.  Recognized key/value pairs are::

        "telnetPort": strports description of the address on which
                      to listen for telnet connections.  If None,
                      no telnet service will be started.

        "sshPort": strports description of the address on which to
                   listen for ssh connections.  If None, no ssh
                   service will be started.

        "namespace": dictionary containing desired initial locals
                     for manhole connections.  If None, an empty
                     dictionary will be used.

        "passwd": Name of a passwd(5)-format username/password file.

        "sshKeyDir": The folder that the SSH server key will be kept in.

        "sshKeyName": The filename of the key.

        "sshKeySize": The size of the key, in bits. Default is 4096.

    @rtype: L{twisted.application.service.IService}
    @return: A manhole service.
    """
    svc = service.MultiService()

    namespace = options['namespace']
    if namespace is None:
        namespace = {}

    checker = checkers.FilePasswordDB(options['passwd'])

    if options['telnetPort']:
        telnetRealm = _StupidRealm(telnet.TelnetBootstrapProtocol,
                                   insults.ServerProtocol,
                                   manhole.ColoredManhole,
                                   namespace)

        telnetPortal = portal.Portal(telnetRealm, [checker])

        telnetFactory = protocol.ServerFactory()
        telnetFactory.protocol = makeTelnetProtocol(telnetPortal)
        telnetService = strports.service(options['telnetPort'],
                                         telnetFactory)
        telnetService.setServiceParent(svc)

    if options['sshPort']:
        sshRealm = manhole_ssh.TerminalRealm()
        sshRealm.chainedProtocolFactory = chainedProtocolFactory(namespace)

        sshPortal = portal.Portal(sshRealm, [checker])
        sshFactory = manhole_ssh.ConchFactory(sshPortal)

        if options['sshKeyDir'] != "  <  USER DATA DIR>":
            keyDir = options['sshKeyDir']
        else:
            from twisted.python._appdirs import getDataDirectory
            keyDir = getDataDirectory()

        keyLocation = filepath.FilePath(keyDir).child(options['sshKeyName'])

        sshKey = keys._getPersistentRSAKey(keyLocation,
                                           int(options['sshKeySize']))
        sshFactory.publicKeys[b"ssh-rsa"] = sshKey
        sshFactory.privateKeys[b"ssh-rsa"] = sshKey

        sshService = strports.service(options['sshPort'], sshFactory)
        sshService.setServiceParent(svc)

    return svc

0 Source : test_application.py
with MIT License
from autofelix

    def testTCP(self):
        s = service.MultiService()
        s.startService()
        factory = protocol.ServerFactory()
        factory.protocol = TestEcho
        TestEcho.d = defer.Deferred()
        t = internet.TCPServer(0, factory)
        t.setServiceParent(s)
        num = t._port.getHost().port
        factory = protocol.ClientFactory()
        factory.d = defer.Deferred()
        factory.protocol = Foo
        factory.line = None
        internet.TCPClient('127.0.0.1', num, factory).setServiceParent(s)
        factory.d.addCallback(self.assertEqual, b'lalala')
        factory.d.addCallback(lambda x : s.stopService())
        factory.d.addCallback(lambda x : TestEcho.d)
        return factory.d


    def test_UDP(self):

0 Source : test_application.py
with MIT License
from autofelix

    def testUNIX(self):
        # FIXME: This test is far too dense.  It needs comments.
        #  -- spiv, 2004-11-07
        s = service.MultiService()
        s.startService()
        factory = protocol.ServerFactory()
        factory.protocol = TestEcho
        TestEcho.d = defer.Deferred()
        t = internet.UNIXServer('echo.skt', factory)
        t.setServiceParent(s)
        factory = protocol.ClientFactory()
        factory.protocol = Foo
        factory.d = defer.Deferred()
        factory.line = None
        internet.UNIXClient('echo.skt', factory).setServiceParent(s)
        factory.d.addCallback(self.assertEqual, b'lalala')
        factory.d.addCallback(lambda x : s.stopService())
        factory.d.addCallback(lambda x : TestEcho.d)
        factory.d.addCallback(self._cbTestUnix, factory, s)
        return factory.d


    def _cbTestUnix(self, ignored, factory, s):

0 Source : tap.py
with MIT License
from autofelix

def makeService(config):
    s = service.MultiService()
    if config['root']:
        root = config['root']
        if config['indexes']:
            config['root'].indexNames = config['indexes']
    else:
        # This really ought to be web.Admin or something
        root = demo.Test()

    if isinstance(root, static.File):
        root.registry.setComponent(interfaces.IServiceCollection, s)

    if config['extraHeaders']:
        root = _AddHeadersResource(root, config['extraHeaders'])

    if config['logfile']:
        site = server.Site(root, logPath=config['logfile'])
    else:
        site = server.Site(root)

    if config["display-tracebacks"]:
        site.displayTracebacks = True

    # Deprecate --notracebacks/-n
    if config["notracebacks"]:
        msg = deprecate._getDeprecationWarningString(
            "--notracebacks", incremental.Version('Twisted', 19, 7, 0))
        warnings.warn(msg, category=DeprecationWarning, stacklevel=2)

    if config['personal']:
        site = makePersonalServerFactory(site)
    for port in config['ports']:
        svc = strports.service(port, site)
        svc.setServiceParent(s)
    return s

0 Source : tap.py
with MIT License
from autofelix

def makeService(config):
    credCheckers = config.get('credCheckers', [])
    wordsRealm = service.InMemoryWordsRealm(config['hostname'])
    wordsPortal = portal.Portal(wordsRealm, credCheckers)

    msvc = MultiService()

    # XXX Attribute lookup on config is kind of bad - hrm.
    for plgName in config.interfacePlugins:
        port = config.get(plgName + '-port')
        if port is not None:
            factory = config.interfacePlugins[plgName].getFactory(wordsRealm, wordsPortal)
            svc = strports.service(port, factory)
            svc.setServiceParent(msvc)

    # This is bogus.  createGroup is async.  makeService must be
    # allowed to return a Deferred or some crap.
    for g in config['groups']:
        wordsRealm.createGroup(g)

    return msvc

0 Source : tap.py
with The Unlicense
from dspray95

def makeService(config):
    s = service.MultiService()
    if config['root']:
        root = config['root']
        if config['indexes']:
            config['root'].indexNames = config['indexes']
    else:
        # This really ought to be web.Admin or something
        root = demo.Test()

    if isinstance(root, static.File):
        root.registry.setComponent(interfaces.IServiceCollection, s)

    if config['extraHeaders']:
        root = _AddHeadersResource(root, config['extraHeaders'])

    if config['logfile']:
        site = server.Site(root, logPath=config['logfile'])
    else:
        site = server.Site(root)

    site.displayTracebacks = not config["notracebacks"]

    if config['personal']:
        site = makePersonalServerFactory(site)
    for port in config['ports']:
        svc = strports.service(port, site)
        svc.setServiceParent(s)
    return s

0 Source : tap.py
with MIT License
from fbla-competitive-events

def makeService(config):
    s = service.MultiService()
    if config['root']:
        root = config['root']
        if config['indexes']:
            config['root'].indexNames = config['indexes']
    else:
        # This really ought to be web.Admin or something
        root = demo.Test()

    if isinstance(root, static.File):
        root.registry.setComponent(interfaces.IServiceCollection, s)

    if config['logfile']:
        site = server.Site(root, logPath=config['logfile'])
    else:
        site = server.Site(root)

    site.displayTracebacks = not config["notracebacks"]

    if not _PY3 and config['personal']:
        personal = strports.service(
            config['port'], makePersonalServerFactory(site))
        personal.setServiceParent(s)
    else:
        if config['https']:
            from twisted.internet.ssl import DefaultOpenSSLContextFactory
            i = internet.SSLServer(int(config['https']), site,
                          DefaultOpenSSLContextFactory(config['privkey'],
                                                       config['certificate']))
            i.setServiceParent(s)
        strports.service(config['port'], site).setServiceParent(s)

    return s


if _PY3:

0 Source : conf.py
with MIT License
from jesopo

def makeService(options):
    from twisted.names.client import getResolver
    resolver = getResolver()
    # HACK: I want a better resolver than the threaded one, and lack a
    # non-terrible place to install it.
    from twisted.internet import reactor
    if not options['keep-resolver']: # pragma: no cover
        reactor.installResolver(resolver)

    # HACK: warn about suboptimal reactor usage
    if not options['force-select']: # pragma: no cover
        from twisted.internet import selectreactor
        if isinstance(reactor, selectreactor.SelectReactor):
            print('The select reactor is probably a bad idea.')
            print('Please use a reactor with better support for lots of fds.')
            print('(-r epoll for example)')
            print('You can bypass this check using --force-select')
            print()
            raise ValueError('unfortunate reactor choice')

    m = service.MultiService()

    checkerFactories = plugin.getCheckerFactories()

    scansets = {}
    for name, d in options['conf']['scansets'].items():
        scans = []
        for args in d['protocols']:
            poolname = args.pop(0)
            checkername = args.pop(0)
            checker = checkerFactories[checkername](*args)
            scans.append((poolname, checker.check))

        actions = d.get('actions', [])
        scansets[name] = scanner.ScanSet(d['timeout'], scans, actions)

    # XXXX the target_blah passing here is horrible, but I intend to
    # make this less global in the future anyway, so laaaaaaaaater
    env = {}
    for k in ['target_ip', 'target_port', 'target_url', 'target_strings',
              'max_bytes', 'bind_address']:
        env[k] = options['conf'].get(k)

    theScanner = scanner.Scanner(
        reactor, resolver, options['conf']['pools'], scansets, env)

    for name, net in options['conf'].get('irc', {}).items():
        # XXX terrible, see also similar complaints in ircpresence.
        # Split this up later.
        factory = ircpresence.Factory(
            net['nick'], net['channel'],
            password=net.get('pass'),
            opername=net.get('opername', net['nick']),
            operpass=net.get('operpass'),
            operkey =net.get('operkey'),
            opermode=net.get('opermode'),
            away=net.get('away'),
            connregex=net.get('connregex'),
            scanner=theScanner,
            masks=options['conf'].get('masks', {}),
            actions=net.get('actions'),
            onconnectmsgs=net.get('onconnectmsgs', ()),
            verbose=options['irc-log'],
            flood_exempt=net.get('flood_exempt', False),
            username=net.get('username'),
            cache_time=net.get("scan-cache-time", 120),
            cache_size=net.get("scan-cache-size", 1_000_000)
            )
        if net.get('ssl', False):

0 Source : ipcontroller.py
with Apache License 2.0
from lumanjiao

def start_controller():
    """
    Start the controller by creating the service hierarchy and starting the reactor.
    
    This method does the following:
    
        * It starts the controller logging
        * In execute an import statement for the controller
        * It creates 2 `foolscap.Tub` instances for the client and the engines
          and registers `foolscap.Referenceables` with the tubs to expose the
          controller to engines and clients.
    """
    config = kernel_config_manager.get_config_obj()
    
    # Start logging
    logfile = config['controller']['logfile']
    if logfile:
        logfile = logfile + str(os.getpid()) + '.log'
        try:
            openLogFile = open(logfile, 'w')
        except:
            openLogFile = sys.stdout
    else:
        openLogFile = sys.stdout
    log.startLogging(openLogFile)
    
    # Execute any user defined import statements
    cis = config['controller']['import_statement']
    if cis:
        try:
            exec cis in globals(), locals()
        except:
            log.msg("Error running import_statement: %s" % cis)
    
    # Delete old furl files unless the reuse_furls is set
    reuse = config['controller']['reuse_furls']
    if not reuse:
        paths = (config['controller']['engine_furl_file'],
            config['controller']['controller_interfaces']['task']['furl_file'],
            config['controller']['controller_interfaces']['multiengine']['furl_file']
        )
        for p in paths:
            if os.path.isfile(p):
                os.remove(p)
    
    # Create the service hierarchy
    main_service = service.MultiService()
    # The controller service
    controller_service = controllerservice.ControllerService()
    controller_service.setServiceParent(main_service)
    # The client tub and all its refereceables
    client_service = make_client_service(controller_service, config)
    client_service.setServiceParent(main_service)
    # The engine tub
    engine_service = make_engine_service(controller_service, config)
    engine_service.setServiceParent(main_service)
    # Start the controller service and set things running
    main_service.startService()
    reactor.run()

def init_config():

0 Source : ipengine.py
with Apache License 2.0
from lumanjiao

def start_engine():
    """
    Start the engine, by creating it and starting the Twisted reactor.
    
    This method does:
    
        * If it exists, runs the `mpi_import_statement` to call `MPI_Init`
        * Starts the engine logging
        * Creates an IPython shell and wraps it in an `EngineService`
        * Creates a `foolscap.Tub` to use in connecting to a controller.
        * Uses the tub and the `EngineService` along with a Foolscap URL
          (or FURL) to connect to the controller and register the engine
          with the controller
    """
    kernel_config = kernel_config_manager.get_config_obj()
    core_config = core_config_manager.get_config_obj()
    

    # Execute the mpi import statement that needs to call MPI_Init
    global mpi
    mpikey = kernel_config['mpi']['default']
    mpi_import_statement = kernel_config['mpi'].get(mpikey, None)
    if mpi_import_statement is not None:
        try:
            exec mpi_import_statement in globals()
        except:
            mpi = None
    else:
        mpi = None
    
    # Start logging
    logfile = kernel_config['engine']['logfile']
    if logfile:
        logfile = logfile + str(os.getpid()) + '.log'
        try:
            openLogFile = open(logfile, 'w')
        except:
            openLogFile = sys.stdout
    else:
        openLogFile = sys.stdout
    log.startLogging(openLogFile)
    
    # Create the underlying shell class and EngineService
    shell_class = import_item(core_config['shell']['shell_class'])
    engine_service = EngineService(shell_class, mpi=mpi)
    shell_import_statement = core_config['shell']['import_statement']
    if shell_import_statement:
        try:
            engine_service.execute(shell_import_statement)
        except:
            log.msg("Error running import_statement: %s" % shell_import_statement)
    
    # Create the service hierarchy
    main_service = service.MultiService()
    engine_service.setServiceParent(main_service)
    tub_service = Tub()
    tub_service.setServiceParent(main_service)
    # This needs to be called before the connection is initiated
    main_service.startService()
    
    # This initiates the connection to the controller and calls
    # register_engine to tell the controller we are ready to do work
    engine_connector = EngineConnector(tub_service)
    furl_file = kernel_config['engine']['furl_file']
    log.msg("Using furl file: %s" % furl_file)
    
    def call_connect(engine_service, furl_file):
        d = engine_connector.connect_to_controller(engine_service, furl_file)
        def handle_error(f):
            # If this print statement is replaced by a log.err(f) I get
            # an unhandled error, which makes no sense.  I shouldn't have
            # to use a print statement here.  My only thought is that
            # at the beginning of the process the logging is still starting up
            print "error connecting to controller:", f.getErrorMessage()
            reactor.callLater(0.1, reactor.stop)
        d.addErrback(handle_error)
    
    reactor.callWhenRunning(call_connect, engine_service, furl_file)
    reactor.run()


def init_config():

0 Source : tap.py
with Apache License 2.0
from lynings

def makeService(config):
    s = service.MultiService()
    if config['root']:
        root = config['root']
        if config['indexes']:
            config['root'].indexNames = config['indexes']
    else:
        # This really ought to be web.Admin or something
        root = demo.Test()

    if isinstance(root, static.File):
        root.registry.setComponent(interfaces.IServiceCollection, s)

    if config['extraHeaders']:
        root = _AddHeadersResource(root, config['extraHeaders'])

    if config['logfile']:
        site = server.Site(root, logPath=config['logfile'])
    else:
        site = server.Site(root)

    site.displayTracebacks = not config["notracebacks"]

    if config['personal']:
        personal = strports.service(
            config['port'], makePersonalServerFactory(site))
        personal.setServiceParent(s)
    else:
        if config['https']:
            from twisted.internet.ssl import DefaultOpenSSLContextFactory
            i = internet.SSLServer(int(config['https']), site,
                          DefaultOpenSSLContextFactory(config['privkey'],
                                                       config['certificate']))
            i.setServiceParent(s)
        strports.service(config['port'], site).setServiceParent(s)

    return s

0 Source : server_tap.py
with MIT License
from magic-wormhole

def makeService(config, channel_db="relay.sqlite", reactor=reactor):
    increase_rlimits()

    parent = MultiService()

    channel_db = create_or_upgrade_channel_db(config["channel-db"])
    usage_db = create_or_upgrade_usage_db(config["usage-db"])
    log_file = (os.fdopen(int(config["log-fd"]), "w")
                if config["log-fd"] is not None
                else None)
    server = make_server(channel_db,
                         allow_list=config["allow-list"],
                         advertise_version=config["advertise-version"],
                         signal_error=config["signal-error"],
                         blur_usage=config["blur-usage"],
                         usage_db=usage_db,
                         log_file=log_file,
                         )
    server.setServiceParent(parent)
    rebooted = time.time()
    def expire():
        now = time.time()
        old = now - CHANNEL_EXPIRATION_TIME
        try:
            server.prune_all_apps(now, old)
        except Exception as e:
            # catch-and-log exceptions during prune, so a single error won't
            # kill the loop. See #13 for details.
            log.msg("error during prune_all_apps")
            log.err(e)
        server.dump_stats(now, rebooted=rebooted)
    TimerService(EXPIRATION_CHECK_PERIOD, expire).setServiceParent(parent)

    log_requests = config["blur-usage"] is None
    site = make_web_server(server, log_requests,
                           config["websocket-protocol-options"])
    ep = endpoints.serverFromString(reactor, config["port"]) # to listen
    StreamServerEndpointService(ep, site).setServiceParent(parent)
    log.msg("websocket listening on ws://HOSTNAME:PORT/v1")

    return parent