def StartEnclaveService(config, enclave): try: http_port = config['EnclaveService']['HttpPort'] http_host = config['EnclaveService']['Host'] storage_url = config['StorageService']['URL'] worker_threads = config['EnclaveService'].get('WorkerThreads', 8) reactor_threads = config['EnclaveService'].get('ReactorThreads', 8) except KeyError as ke: logger.error('missing configuration for %s', str(ke)) sys.exit(-1) logger.info('enclave service started on %s:%s', http_host, http_port) logger.info('verifying_key: %s', enclave.verifying_key) logger.info('encryption_key: %s', enclave.encryption_key) logger.info('enclave_id: %s', enclave.enclave_id) logger.info('storage service: %s', storage_url) thread_pool = ThreadPool(minthreads=1, maxthreads=worker_threads) thread_pool.start() reactor.addSystemEventTrigger('before', 'shutdown', thread_pool.stop) root = Resource() root.putChild( b'info', WSGIResource(reactor, thread_pool, AppWrapperMiddleware(InfoApp(enclave, storage_url)))) root.putChild( b'initialize', WSGIResource(reactor, thread_pool, AppWrapperMiddleware(InitializeApp(enclave)))) root.putChild( b'invoke', WSGIResource(reactor, thread_pool, AppWrapperMiddleware(InvokeApp(enclave)))) root.putChild( b'verify', WSGIResource(reactor, thread_pool, AppWrapperMiddleware(VerifyApp(enclave)))) site = Site(root, timeout=60) site.displayTracebacks = True reactor.suggestThreadPoolSize(reactor_threads) signal.signal(signal.SIGQUIT, __shutdown__) signal.signal(signal.SIGTERM, __shutdown__) endpoint = TCP4ServerEndpoint(reactor, http_port, backlog=32, interface=http_host) endpoint.listen(site)
def run(): structlog.configure( processors=[ structlog.processors.StackInfoRenderer(), structlog.twisted.JSONRenderer() ], context_class=dict, logger_factory=structlog.twisted.LoggerFactory(), wrapper_class=structlog.twisted.BoundLogger, cache_logger_on_first_use=True, ) # grab all of the events that are dispatched to stdlib logger # new relic uses this. handler = logging.StreamHandler(sys.stdout) root_logger = logging.getLogger() root_logger.addHandler(handler) # start the twisted logger twLog.startLogging(sys.stdout) # api is the WSGI resource returned by Falcon. api = falcon.API() api.add_route('/quote', QuoteResource()) app = newrelic.agent.WSGIApplicationWrapper(api) resource = WSGIResource(reactor, reactor.getThreadPool(), app) site = Site(resource) reactor.listenTCP(port=8713, factory=site) reactor.run()
def __init__(self, wsgiapp, wsprotocol, port=PORT): ''' Start protocol factory and reactor ''' self.app = wx.GetApp() ip = self.app.IP self.ip = "127.0.0.1" if ip is None else ip self.port = port self.url = "ws://%s:%s" % (self.ip, self.port) # Set Protocol factory self.factory = websocket.WebSocketServerFactory(self.url) self.factory.protocol = wsprotocol self.root = WSGIRoot() self.root.WSGI = WSGIResource(reactor, reactor.getThreadPool(), wsgiapp) self.root.putChild(b"ws", resource.WebSocketResource(self.factory)) static = File(os.path.join(self.app.path['www'], 'static')) self.root.putChild(b"static", static) # Only one File #self.root.putChild(b"documentation", File( os.path.join(self.app.path['www'], 'documentation') ) ) self.site = Site(self.root) # Use the existing reactor reactor.listenTCP(self.port, self.site) echo("Web Server: Starting at %s" % self.root) echo("Websockets Server: Starting at %s" % self.url) topic = "ws.local" self.topic = topic self.factory.protocol.topic = topic self.Initialize()
def __init__(self, core): thread_pool = ThreadPool() thread_pool.start() reactor.addSystemEventTrigger('after', 'shutdown', thread_pool.stop) application = get_flask_application(core) wsgi_resource = WSGIResource(reactor, thread_pool, application) Site.__init__(self, wsgi_resource)
def createService(sname, config): from anchore_engine.services.policy_engine.application import application flask_site = WSGIResource(reactor, reactor.getThreadPool(), application) root = anchore_engine.services.common.getAuthResource( flask_site, sname, config) return (anchore_engine.services.common.createServiceAPI( root, sname, config))
def create_server(app, port): ## # create a Twisted Web resource for our WebSocket server ## ws_factory = WebSocketServerFactory(u"ws://127.0.0.1:5000", debug=app.debug, debugCodePaths=app.debug) ws_factory.protocol = NotificationService ws_resource = WebSocketResource(ws_factory) ## # create a Twisted Web WSGI resource for our Flask server ## wsgi_resource = WSGIResource(reactor, reactor.getThreadPool(), app) ## # create a root resource serving everything via WSGI/Flask, but # the path "/ws" served by our WebSocket stuff ## root_resource = WSGIRootResource(wsgi_resource, {'notification-service': ws_resource}) ## # create a Twisted Web Site and run everything ## site = Site(root_resource) reactor.listenTCP(port, site) reactor.run()
def run_server(self): # import libs from twisted.internet import endpoints from twisted.internet import reactor from twisted.internet import task from twisted.web import server from twisted.web.wsgi import WSGIResource # create default configuration self.make_config() # We will be using Twisted HTTP server so let's # disable the CherryPy's HTTP server entirely cherrypy.server.unsubscribe() # Publish periodically onto the 'main' channel as the bus mainloop would do task.LoopingCall(lambda: cherrypy.engine.publish('main')).start(0.1) # create SSL server from string https_server = endpoints.serverFromString(reactor, self.get_server_str()) # Tie our app to Twisted reactor.addSystemEventTrigger('after', 'startup', cherrypy.engine.start) reactor.addSystemEventTrigger('before', 'shutdown', cherrypy.engine.exit) resource = WSGIResource(reactor, reactor.getThreadPool(), wsgi.application) site = server.Site(resource) https_server.listen(site) reactor.run()
def run(port=8880): print "running windpyfoam web server %s" % __version__ print "listening on port %s" % port root = WSGIResource(reactor, reactor.getThreadPool(), app) factory = Site(root) reactor.listenTCP(port, factory) reactor.run()
def installApplication(self, application): """Install the WSGI application into the Twisted site. It's installed as a child with path "MAAS". This matches the default front-end configuration (i.e. Apache) so that there's no need to force script names. """ # Setup resources to process paths that twisted handles. metadata = Resource() metadata.putChild(b'status', StatusHandlerResource(self.status_worker)) maas = Resource() maas.putChild(b'metadata', metadata) maas.putChild(b'static', File(settings.STATIC_ROOT)) maas.putChild( b'ws', WebSocketsResource(lookupProtocolForFactory(self.websocket))) root = Resource() root.putChild(b'', Redirect(b"MAAS/")) root.putChild(b'MAAS', maas) # Setup the resources to process paths that django handles. underlay_maas = ResourceOverlay( WSGIResource(reactor, self.threadpool, application)) underlay_root = Resource() underlay_root.putChild(b'MAAS', underlay_maas) underlay_site = Site(underlay_root, logFormatter=reducedWebLogFormatter) underlay_site.requestFactory = CleanPathRequest # Setup the main resource as the twisted handler and the underlay # resource as the django handler. self.site.resource = root self.site.underlay = underlay_site
def run_twisted_wsgi(): from twisted.web.server import Site from twisted.web.wsgi import WSGIResource resource = WSGIResource(reactor, reactor.getThreadPool(), app) site = Site(resource) reactor.listenTCP(5000, site)
def setup_twisted(self): """Setting up the twisted service. Soaplib has to be setup first.""" self.resource = WSGIResource(reactor, reactor.getThreadPool(), self.wsgi_application) self.root = Resource() self.root.putChild(self.soapchildpath, self.resource) self.site = Site(self.root)
def run_twisted(apps, port, with_static_file_server=True): """Twisted wrapper for the rpclib.server.wsgi.Application Takes a list of tuples containing application, url pairs, and a port to to listen to. """ static_dir = os.path.abspath(".") if with_static_file_server: logging.info("registering static folder %r on /" % static_dir) root = twisted.web.static.File(static_dir) else: root = Resource() for app, url in apps: resource = WSGIResource(reactor, reactor, app) logging.info("registering %r on /%s" % (app, url)) root.putChild(url, resource) site = twisted.web.server.Site(root) reactor.listenTCP(port, site) logging.info("listening on: 0.0.0.0:%d" % port) return reactor.run()
def main(): my_flask_endpoint = FlaskEndpoint() # Since Flask is stateful, using multiple apps is generally not a common practice (although it is possible) # As a consequence, blueprints are employed to register handles in projects with multiple modules app.register_blueprint(my_flask_endpoint.flask_blueprint) root_endpoint = Resource() # A flask app can only be used with Twisted via the WSGIResource. Its handlers do not support asynchronous code. flask_resource = WSGIResource(reactor, reactor.getThreadPool(), app) root_endpoint.putChild(b"flask", flask_resource) reactor.listenTCP(8081, Site(root_endpoint), interface="localhost") # Note the '/' character at the end of the endpoint this is essential for a correct routing since the handler is # registered at '/math/' not '/math' req_1 = request_wrapper("flask/flask_endpoint/math/", { "op": "mul", "a": 10, "b": 31 }) req_1.addCallback(lambda response: print(response)) # There is no '/' character at the end of the uri because the handler was registered under '/echo' req_2 = request_wrapper("flask/flask_endpoint/echo", {"msg": "Hello world!"}) req_2.addCallback(lambda response: print(response))
def makeService(self, options): config = options import cap import sys sys.path.insert(1,cap.__path__[0]) del sys.modules["cap"] os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cap.settings") mysql_url = options["mysql_url"].strip() try: a, b = mysql_url.split(":") mysql_host = a mysql_port, mysql_db = b.split("/") mysql_port = int(mysql_port) except: print "mysql相关配置错误" raise Exception("mysql相关配置错误") else: mysql_user = options["mysql_user"] mysql_password = options["mysql_password"] os.config = [mysql_host,mysql_port,mysql_db,mysql_user,mysql_password] from django.core.handlers.wsgi import WSGIHandler application = WSGIHandler() resource = WSGIResource(reactor, reactor.getThreadPool(), application) ui_service=TCPServer(9912,server.Site(resource),interface=config["host"]) return ui_service
def __init__(self, reactor, configuration, callbacks): """ Initialize the HTTP server with the specified `configuration`. :param reactor: The reactor to bind to. :param configuration: The configuration, as given by the FreeLAN core. :param callbacks: The callbacks, as given by the FreeLAN core. """ @APP.before_request def associate_http_server(): g.http_server = self self.configuration = configuration self.callbacks = callbacks self.app = APP self.resource = WSGIResource( reactor, reactor.getThreadPool(), self.app, ) self.site = Site(self.resource) hostname, port = parse_endpoint(configuration['listen_on']) reactor.listenTCP(port, self.site, interface=hostname) self.app.secret_key = self.configuration['secret_key']
def main(argv: List[str]) -> Any: from twisted.internet import reactor from twisted.web.server import Site from twisted.web.wsgi import WSGIResource from twisted.python import log parser = argparse.ArgumentParser() parser.add_argument("-c", "--cache", action="store_true", help="use local repo cache") parser.add_argument("-p", "--port", type=int, default=8160, help="port number") parser.add_argument("-d", "--debug", action="store_true") args = parser.parse_args() app.config["CACHE_LOCAL"] = args.cache print("http://localhost:%d" % args.port) if args.debug: app.debug = True log.startLogging(sys.stdout) wsgiResource = WSGIResource(reactor, reactor.getThreadPool(), app) site = Site(wsgiResource) reactor.listenTCP(args.port, site) reactor.run()
def run(): argv = sys.argv[1:] if argv: config_file_path = argv[0] else: caller_file = inspect.getouterframes(inspect.currentframe())[1][1] caller_file = os.path.realpath(caller_file) buildout_dir = os.path.dirname(os.path.dirname(caller_file)) config_file_path = os.path.join(buildout_dir, 'parts', 'etc', 'config.ini') if not os.path.isfile(config_file_path): print u'Path to config file must be given as a single parameter, for example "bin/run parts/etc/config.ini"' return paster.setup_logging(config_file_path) settings = paster.get_appsettings(config_file_path) app = main(None, **settings) from intranet3 import cron if not config.get('CRON_DISABLE'): cron.run_cron_tasks() full_config_path = os.path.abspath(config_file_path) server_config = ConfigParser.ConfigParser() server_config.readfp(open(full_config_path)) port = server_config.getint('server:main', 'port') host = server_config.get('server:main', 'host') resource = WSGIResource(reactor, reactor.getThreadPool(), app) site = server.Site(resource) reactor.listenTCP(port, site) reactor.run()
def installApplication(self, application): """Install the WSGI application into the Twisted site. It's installed as a child with path "MAAS". This matches the default front-end configuration (i.e. Apache) so that there's no need to force script names. """ # Setup resources to process paths that twisted handles. metadata = Resource() metadata.putChild(b"status", StatusHandlerResource(self.status_worker)) maas = Resource() maas.putChild(b"metadata", metadata) maas.putChild( b"ws", WebSocketsResource(lookupProtocolForFactory(self.websocket)) ) # /MAAS/r/{path} and /MAAS/l/{path} are all resolved by the new MAAS UI # react app, and legacy angularjs app respectively. # If any paths do not match then its routed to index.html in the new # UI code as it uses HTML 5 routing. maas.putChild(b"r", DefaultFallbackFile(settings.STATIC_ROOT)) maas.putChild(b"l", DefaultFallbackFile(settings.STATIC_ROOT)) # Redirect /MAAS to react app maas.putChild(b"", Redirect(b"/MAAS/r/")) # Setup static resources maas.putChild( b"assets", NoListingFile(os.path.join(settings.STATIC_ROOT, "assets")), ) # Setup static docs maas.putChild( b"docs", DocsFallbackFile(os.path.join(settings.STATIC_ROOT, "docs")), ) root = Resource() root.putChild(b"", Redirect(b"MAAS/")) root.putChild(b"MAAS", maas) # Setup the resources to process paths that django handles. underlay_maas = ResourceOverlay( WSGIResource(reactor, self.threadpool, application) ) underlay_root = Resource() underlay_root.putChild(b"MAAS", underlay_maas) underlay_site = Site( underlay_root, logFormatter=reducedWebLogFormatter ) underlay_site.requestFactory = CleanPathRequest # Setup the main resource as the twisted handler and the underlay # resource as the django handler. self.site.resource = root self.site.underlay = underlay_site
def createService(sname, config): global monitor_threads, monitors, servicename try: application = connexion.FlaskApp(__name__, specification_dir='swagger/') flask_app = application.app flask_app.url_map.strict_slashes = False anchore_engine.subsys.metrics.init_flask_metrics(flask_app, servicename=servicename) application.add_api('swagger.yaml') except Exception as err: traceback.print_exc() raise err try: myconfig = config['services'][sname] servicename = sname except Exception as err: raise err try: kick_timer = int(myconfig['cycle_timer_seconds']) except: kick_timer = 1 doapi = False try: if myconfig['listen'] and myconfig['port'] and myconfig['endpoint_hostname']: doapi = True except: doapi = False kwargs = {} kwargs['kick_timer'] = kick_timer kwargs['monitors'] = monitors kwargs['monitor_threads'] = monitor_threads kwargs['servicename'] = servicename if doapi: # start up flask service flask_site = WSGIResource(reactor, reactor.getThreadPool(), application=flask_app) realroot = Resource() realroot.putChild(b"v1", anchore_engine.services.common.getAuthResource(flask_site, sname, config)) realroot.putChild(b"health", anchore_engine.services.common.HealthResource()) # this will rewrite any calls that do not have an explicit version to the base path before being processed by flask root = rewrite.RewriterResource(realroot, default_version_rewrite) #root = anchore_engine.services.common.getAuthResource(flask_site, sname, config) ret_svc = anchore_engine.services.common.createServiceAPI(root, sname, config) # start up the monitor as a looping call lc = LoopingCall(anchore_engine.services.common.monitor, **kwargs) lc.start(1) else: # start up the monitor as a timer service svc = internet.TimerService(1, anchore_engine.services.common.monitor, **kwargs) svc.setName(sname) ret_svc = svc return (ret_svc)
def __init__(self, plugin): """ Constructor """ Resource.__init__(self) self.wsgi = WSGIResource(plugin.service.reactor, plugin.service.reactor.getThreadPool(), plugin.app)
def startTwistedServer(self): application = self.make_app() resource = WSGIResource(reactor, reactor.getThreadPool(), application) site = Site(resource) self.port = reactor.listenTCP(0, site, interface='127.0.0.1') host = self.port.getHost() self.server_address = (host.host, host.port) self.addCleanup(self.port.stopListening)
def createService(sname, config): global app flask_site = WSGIResource(reactor, reactor.getThreadPool(), app) root = anchore_engine.services.common.getAuthResource( flask_site, sname, config) return (anchore_engine.services.common.createServiceAPI( root, sname, config))
def getSite(): root = SharedRoot() resource = WSGIResource(reactor, reactor.getThreadPool(), application) root.wsgi = resource return server.Site(root)
def run_twisted_wsgi(): from twisted.internet import reactor from twisted.web.server import Site from twisted.web.wsgi import WSGIResource resource = WSGIResource(reactor, reactor.getThreadPool(), APP) site = Site(resource) reactor.listenTCP(5000, site) reactor.run(**reactor_args)
def start(self): root = SharedRoot() root.WSGI = WSGIResource(reactor, reactor.getThreadPool(), self.app) self.webserver = server.Site(root) reactor.listenTCP(self.conf.HTTP_PORT, self.webserver) reactor.callLater(0, self.browser.show) reactor.run()
def run(): config = get_config() app = init_api_client() flask_site = WSGIResource(reactor, reactor.getThreadPool(), app) site = Site(flask_site) reactor.listenTCP(config.get('application', 'port'), site) reactor.run()
def runtwisted(config=None): """ Run the Twisted server. """ globalLogBeginner.beginLoggingTo( [FileLogObserver(sys.stdout, lambda _: formatEvent(_) + "\n")]) threadpool = ThreadPool(maxthreads=30) app = api.makeapp(config=config) wsgi_app = WSGIResource(reactor, threadpool, app) class OptimaResource(Resource): isLeaf = True def __init__(self, wsgi): self._wsgi = wsgi def render(self, request): request.prepath = [] request.postpath = ['api'] + request.postpath[:] r = self._wsgi.render(request) request.responseHeaders.setRawHeaders( b'Cache-Control', [b'no-cache', b'no-store', b'must-revalidate']) request.responseHeaders.setRawHeaders(b'expires', [b'0']) return r # If we have a full path for the client directory, use that directory. if os.path.isabs(config.CLIENT_DIR): clientDirTarget = config.CLIENT_DIR # Otherwise (we have a relative path), use it (correcting so it is with # respect to the sciris repo directory). else: clientDirTarget = '%s%s%s' % (os.pardir, os.sep, config.CLIENT_DIR) base_resource = File('%s%sdist%s' % (clientDirTarget, os.sep, os.sep)) base_resource.putChild( 'dev', File('%s%ssrc%s' % (clientDirTarget, os.sep, os.sep))) base_resource.putChild('api', OptimaResource(wsgi_app)) site = Site(base_resource) try: port = str(sys.argv[1]) except IndexError: port = "8091" # Start the threadpool now, shut it down when we're closing threadpool.start() reactor.addSystemEventTrigger('before', 'shutdown', threadpool.stop) endpoint = serverFromString(reactor, "tcp:port=" + port) endpoint.listen(site) reactor.run()
def run_twisted(app, port=8000, *args, **kwargs): from twisted.web.wsgi import WSGIResource from twisted.internet import reactor from twisted.web import wsgi, server resource = WSGIResource(reactor, reactor.getThreadPool(), app) reactor.listenTCP(port, server.Site(resource)) log.info("Server running on port %d" % (port)) reactor.run()
def __init__(self, app, options=None): """ :param app: :param options: """ BaseApplication.__init__(self, app, options) resource = WSGIResource(reactor, reactor.getThreadPool(), self.application) self._site = Site(resource)
def __init__(self, app, port, host, debug): super(TwistedApplicationServer, self).__init__(app, port, host, debug) self.reactor = reactor self.thread_pool = ThreadPool(5, 40) self.resource = WSGIResource(self.reactor, self.thread_pool, self.app) self.reactor.addSystemEventTrigger('after', 'shutdown', self.thread_pool.stop)