def run(self, *args, **kwargs): worker_threads = kwargs['worker-threads'] assert worker_threads >= 0 ping_address = kwargs['ping-address'] assert ping_address data_address = kwargs['data-address'] assert data_address poll_timeout = kwargs['poll-timeout'] assert poll_timeout workers = [] def start(): while any(map(lambda w: not w.stopped, workers)): for worker in workers: worker.stop() while len(workers) > 0: workers.pop() for _ in range(worker_threads): workers.append( backend.Worker(ping_address=ping_address, data_address=data_address, poll_timeout=poll_timeout)) workers[-1].start() try: run_with_reloader(start) except KeyboardInterrupt: while any(map(lambda w: not w.stopped, workers)): for worker in workers: worker.stop()
def main(args=sys.argv[1:]): parser = argparse.ArgumentParser(description='Runnerly Tokendealer') parser.add_argument('--fd', type=int, default=None) parser.add_argument('--config-file', help='Config file', type=str, default=_SETTINGS) args = parser.parse_args(args=args) app = create_app(args.config_file) app.static_folder = _STATIC_FOLDER fs_loader = jinja2.FileSystemLoader([_TMPL_DIR]) app.jinja_loader = jinja2.ChoiceLoader([app.jinja_loader, fs_loader]) host = app.config.get('host', '0.0.0.0') port = app.config.get('port', 5000) debug = app.config.get('DEBUG', False) signal.signal(signal.SIGINT, _quit) signal.signal(signal.SIGTERM, _quit) def runner(): if args.fd is not None: # use chaussette httpd = make_server(app, host='fd://%d' % args.fd) httpd.serve_forever() else: app.run(debug=debug, host=host, port=port) if not debug: runner() else: run_with_reloader(runner)
def run(app, host=None, port=None, debug=None, logger=logger): monkey.patch_all() address = get_address(host, port, app.config['SERVER_NAME']) if debug is not None: app.debug = debug if app.debug: #app.wsgi_app = DebuggedApplication(app.wsgi_app, evalex=True) server = WebSocketServer(address, app.wsgi_app, debug=debug) server._logger = logger logger.setLevel(logging.INFO) def run_server(): server.serve_forever() if os.environ.get('WERKZEUG_RUN_MAIN') != 'true': server.logger.info(' * Running on http://%s:%d/' % address) server.logger.info(' * WebSocket enabled') run_with_reloader(run_server) else: server = WebSocketServer(address, app.wsgi_app, debug=debug) server._logger = logger server.serve_forever() return server
def runserver(host="127.0.0.1", port=8080): """Run a gevent-based WSGI server.""" port = int(port) wrapped_app = app if app.config.get("DEBUG", True): #it is necessary to do this for the debug to work, since the classic # flask debugger does not operate when running the app from # a WSGIServer() call. wrapped_app = DebuggedApplication(app) server = WSGIServer( listener=(host, port), application=wrapped_app, ) def serve(): print(" * Running on http://%s:%d/" % (host, port)) server.serve_forever() if app.debug: # the watchdog reloader (with watchdog==0.8.3) appears to hang on # Windows 8.1, so we're using stat instead run_with_reloader( serve, reloader_type="stat" if sys.platform == "win32" else "auto") else: serve()
def runserver(ip, port, debug=False): # pragma: no cover from cherrypy import wsgiserver from werkzeug.serving import run_with_reloader app = ErrorMiddleware(make_app(debug=debug)) server = wsgiserver.CherryPyWSGIServer((ip, port), app, numthreads=30) run_with_reloader(server.start)
def serve(host, port, debug): quasimodo.app.debug = quasimodo.app.testing = debug if quasimodo.app.debug: run_with_reloader(functools.partial(quasimodo.run, host, port)) else: quasimodo.run(host, port)
def run(self, app, host=None, port=None, **kwargs): if host is None: host = '127.0.0.1' if port is None: server_name = app.config['SERVER_NAME'] if server_name and ':' in server_name: port = int(server_name.rsplit(':', 1)[1]) else: port = 5000 # don't allow override of resource, otherwise allow SocketIOServer # kwargs to be passed through kwargs.pop('resource', None) self.server = SocketIOServer((host, port), app.wsgi_app, resource='socket.io', **kwargs) if app.debug: def run_server(): self.server.serve_forever() if os.environ.get('WERKZEUG_RUN_MAIN') != 'true': _log('info', ' * Running on http://%s:%d/' % (host, port)) run_with_reloader(run_server) else: self.server.serve_forever()
def main5(): from gevent import pywsgi from werkzeug.debug import DebuggedApplication dapp = DebuggedApplication( app, evalex= True) from werkzeug.serving import run_with_reloader server = pywsgi.WSGIServer( ( '127.0.0.1', 5000 ), dapp ) run_with_reloader( server ).serve_forever()
def run(self, *args, **kwargs): worker_threads = kwargs["worker-threads"] assert worker_threads >= 0 ping_address = kwargs["ping-address"] assert ping_address data_address = kwargs["data-address"] assert data_address poll_timeout = kwargs["poll-timeout"] assert poll_timeout workers = [] def start(): while any(map(lambda w: not w.stopped, workers)): for worker in workers: worker.stop() while len(workers) > 0: workers.pop() for _ in range(worker_threads): workers.append( backend.Worker(ping_address=ping_address, data_address=data_address, poll_timeout=poll_timeout) ) workers[-1].start() try: run_with_reloader(start) except KeyboardInterrupt: while any(map(lambda w: not w.stopped, workers)): for worker in workers: worker.stop()
def __call__(self, app, host, port, use_debugger, use_reloader, threaded, processes, passthrough_errors): if use_debugger is None: use_debugger = app.debug if use_debugger is None: use_debugger = True if sys.stderr.isatty(): print("Debugging is on. DANGER: Do not allow random users to connect to this server.", file=sys.stderr) if use_reloader is None: use_reloader = app.debug if use_debugger: from werkzeug.debug import DebuggedApplication app = DebuggedApplication(app, True) def run(): from gevent.wsgi import WSGIServer gws = WSGIServer((host, port), app) gws.base_env['wsgi.multithread'] = threaded gws.base_env['wsgi.multiprocess'] = processes > 0 gws.serve_forever() if use_reloader: from werkzeug.serving import run_with_reloader run_with_reloader(run) else: run()
def run(app: flask.Flask, *, host='127.0.0.1', port=None, debug=False, loop=None): """Run Flask application on aiohttp :param app: Flask application :param host: host name or ip :param port: port (default is 5000) :param debug: debug? """ # Check initialization status of flask app. if getattr(app, 'aiohttp_app', None) is None: raise RuntimeError( "This application is not initialized for Flask-aiohttp. " "Please initialize the app by `aio.init_app(app)`.") # Configure args if port is None: server_name = app.config['SERVER_NAME'] if server_name and ':' in server_name: port = int(server_name.rsplit(':', 1)[-1]) else: port = 5000 loop = loop or asyncio.get_event_loop() # Define run_server def run_server(): # run_server can be called in another thread asyncio.set_event_loop(loop) coroutine = loop.create_server( app.aiohttp_app.make_handler(), host, port) loop.run_until_complete(coroutine) try: loop.run_forever() except KeyboardInterrupt: pass # Configure logging file_handler = logging.StreamHandler() app.logger.setLevel(logging.INFO) app.logger.addHandler(file_handler) if debug: # Logging app.logger.setLevel(logging.DEBUG) # Wrap WSGI app with werkzeug debugger. app.wsgi_app = wrap_wsgi_middleware(DebuggedApplication)( app.wsgi_app) if os.environ.get('WERKZEUG_RUN_MAIN') != 'true': app.logger.info(' * Running on http://{}:{}/' .format(host, port)) # Run with reloader run_with_reloader(run_server) else: app.logger.info(' * Running on http://{}:{}/'.format(host, port)) run_server()
def runServer(self): """Starts up the server. It (will) support different config options via the config plugin.""" config = pm.getService("config") debug = config.get("flask.debug") cFCGI = config.get("flask.fcgi") host = config.get("flask.bind") app_port = config.get("flask.app_port") fcgi_port = config.get("flask.fcgi_port") must_have_client_cert = config.get("flask.force_client_cert") if cFCGI: logger.info("registering fcgi server at %s:%i", host, fcgi_port) from flup.server.fcgi import WSGIServer WSGIServer(self._app, bindAddress=(host, fcgi_port)).run() else: logger.info("registering app server at %s:%i", host, app_port) # do the following line manually, so we can intervene and adjust the ssl context # self._app.run(host=host, port=app_port, ssl_context='adhoc', debug=debug, request_handler=ClientCertHTTPRequestHandler) # the code from flask's `run...` # see https://github.com/mitsuhiko/flask/blob/master/flask/app.py options = {} try: # now the code from werkzeug's `run_simple(host, app_port, self._app, **options)` # see https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/serving.py from werkzeug.debug import DebuggedApplication import socket application = DebuggedApplication(self._app, True) # Set up an SSL context cert_path = expand_eisoil_path(config.get("delegatetools.trusted_cert_path")) cert_key_path = expand_eisoil_path(config.get("delegatetools.trusted_cert_keys_path")) context = SSL.Context(SSL.SSLv23_METHOD) context_crt = os.path.join(cert_path, "ch-cert.pem") context_key = os.path.join(cert_key_path, "ch-key.pem") try: context.use_certificate_file(context_crt) context.use_privatekey_file(context_key) except Exception as e: logger.critical("error starting flask server. Cert or key is missing under %s", cert_path) sys.exit(e) def inner(): # server = serving.make_server(host, app_port, self._app, False, 1, ClientCertHTTPRequestHandler, False, 'adhoc') server = serving.make_server(host, app_port, self._app, False, 1, ClientCertHTTPRequestHandler, False, ssl_context=context) # The following line is the reason why I copied all that code! if must_have_client_cert: server.ssl_context.set_verify(SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT, lambda a,b,c,d,e: True) # That's it server.serve_forever() address_family = serving.select_ip_version(host, app_port) test_socket = socket.socket(address_family, socket.SOCK_STREAM) test_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) test_socket.bind((host, app_port)) test_socket.close() serving.run_with_reloader(inner, None, 1) finally: self._app._got_first_request = False
def serve(reloader): def run(): pywsgi.WSGIServer(('0.0.0.0', 8686), GamesKeeper.app).serve_forever() if reloader: run_with_reloader(run) else: run()
def serve(reloader): def run(): wsgi.WSGIServer(('0.0.0.0', 8686), rowboat.app).serve_forever() if reloader: run_with_reloader(run) else: run()
def main(): p = parser() args = p.parse_args() def helper(): run_args(args) if args.reload: run_with_reloader(helper) else: helper()
def serve(reloader): """Serve the server with or without reloader""" def run(): wsgi.WSGIServer(('0.0.0.0', 8080), todoAPI).serve_forever() if reloader: run_with_reloader(run) else: run()
def callback(info, *args, **kwargs): if self.with_reloader: app = info.load_app() if app.debug: def inner(): return self.command_callback(info, *args, **kwargs) run_with_reloader(inner, extra_files=get_reloader_extra_files()) return self.command_callback(info, *args, **kwargs)
def runserver(): "run the server" from app import app # create_app has been run by now. def run_server(): http_server = WSGIServer((app.config["HOST"], app.config["PORT"]), DebuggedApplication(app)) http_server.serve_forever() run_with_reloader(run_server)
def run(app, port): if app.debug: logger.info('blogsley running in debug mode') def debugfn(): debug_server(app, port) run_with_reloader(debugfn) else: run_server(app, port)
def runServer(self): """Starts up the server. It (will) support different config options via the config plugin.""" config = pm.getService("config") debug = config.get("flask.debug") cFCGI = config.get("flask.fcgi") host = config.get("flask.bind") app_port = config.get("flask.app_port") fcgi_port = config.get("flask.fcgi_port") must_have_client_cert = config.get("flask.force_client_cert") if cFCGI: logger.info("registering fcgi server at %s:%i", host, fcgi_port) from flup.server.fcgi import WSGIServer WSGIServer(self._app, bindAddress=(host, fcgi_port)).run() else: logger.info("registering app server at %s:%i", host, app_port) # this workaround makes sure that the client cert can be acquired later (even when running the development server) # copied all this stuff from the actual flask implementation, so we can intervene and adjust the ssl context # self._app.run(host=host, port=app_port, ssl_context='adhoc', debug=debug, request_handler=ClientCertHTTPRequestHandler) # the code from flask's `run...` # see https://github.com/mitsuhiko/flask/blob/master/flask/app.py options = {} try: # now the code from werkzeug's `run_simple(host, app_port, self._app, **options)` # see https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/serving.py from werkzeug.debug import DebuggedApplication import socket application = DebuggedApplication(self._app, True) def inner(): server = serving.make_server(host, app_port, self._app, False, 1, ClientCertHTTPRequestHandler, False, 'adhoc') # The following line is the reason why I copied all that code! if must_have_client_cert: server.ssl_context.set_verify( SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT, lambda a, b, c, d, e: True) # That's it server.serve_forever() address_family = serving.select_ip_version(host, app_port) test_socket = socket.socket(address_family, socket.SOCK_STREAM) test_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) test_socket.bind((host, app_port)) test_socket.close() serving.run_with_reloader(inner, None, 1) finally: self._app._got_first_request = False
def run(info, host, port, reload, debugger, eager_loading, with_threads): """Runs a local development server for the Flask-SocketIO-self application. The reloader and debugger are by default enabled if the debug flag of Flask is enabled and disabled otherwise. """ debug = get_debug_flag() if reload is None: reload = bool(debug) if debugger is None: debugger = bool(debug) if eager_loading is None: eager_loading = not reload # Extra startup messages. This depends a bit on Werkzeug internals to # not double execute when the reloader kicks in. if os.environ.get('WERKZEUG_RUN_MAIN') != 'true': # If we have an import path we can print it out now which can help # people understand what's being served. If we do not have an # import path because the app was loaded through a callback then # we won't print anything. if info.app_import_path is not None: print(' * Serving Flask-SocketIO-self app "%s"' % info.app_import_path) if debug is not None: print(' * Forcing debug mode %s' % (debug and 'on' or 'off')) else: # if this is the child process of the reloader, then make sure we don't # start the reloader once again reload = False def run_server(): app = info.load_app() if 'socketio' not in app.extensions: # flask-socketio is installed, but it isn't in this application # so we invoke Flask's original run command run_index = sys.argv.index('run') sys.argv = sys.argv[run_index:] return run_command() socketio = app.extensions['socketio'] socketio.run(app, host=host, port=port, debug=debugger, use_reloader=False, log_output=debugger) if reload: from werkzeug.serving import run_with_reloader run_with_reloader(run_server) else: run_server()
def run_wsgi_app_with_cherrypy(app, default_port=8081): parser = argparse.ArgumentParser( description='Milo Web Service') parser.add_argument( '--ip', nargs='?', default='localhost', help="defaults to %(default)s") parser.add_argument( '-p', '--port', type=int, default=default_port, help="defaults to %(default)s") parser.add_argument( '-d', '--debug', action='store_true', default=False, help="show traceback interpreter on error") parser.add_argument( '-n', '--threads', type=int, default=30, help="number of threads to use") parser.add_argument( '--port-start', type=int, default=None, help="starting port number") parser.add_argument( '--port-offset', type=int, default=None, help="0 based offset to be added to start-port to determine port") args = parser.parse_args() if args.port_start is not None and args.port_offset is not None: port = args.port_start + args.port_offset else: port = args.port if args.debug: from werkzeug.debug import DebuggedApplication from wsgiref.validate import validator app = DebuggedApplication(app, evalex=True) app = validator(app) from cherrypy import wsgiserver import signal server = wsgiserver.CherryPyWSGIServer( (args.ip, port), app, numthreads=args.threads) logger.info('Bringing up server on port %s', port) if args.debug: from werkzeug.serving import run_with_reloader run_with_reloader(server.start) else: def die_die_die_cherrypy(signum, frame): print die_message server.stop() signal.signal(signal.SIGINT, die_die_die_cherrypy) signal.signal(signal.SIGTERM, die_die_die_cherrypy) server.start()
def start(mg): """Start WEB UI""" global manager manager = mg # Create sqlalchemy session for Flask usage global db_session db_session = scoped_session( sessionmaker(autocommit=False, autoflush=False, bind=manager.engine)) if db_session is None: raise Exception('db_session is None') load_ui_plugins() # quick hack: since ui plugins may add tables to SQLAlchemy too and they're not initialized because create # was called when instantiating manager .. so we need to call it again from flexget.manager import Base Base.metadata.create_all(bind=manager.engine) app.register_blueprint(api) app.register_blueprint(api_schema) fire_event('webui.start') # Start Flask app.secret_key = os.urandom(24) set_exit_handler(stop_server) log.info('Starting server on port %s' % manager.options.webui.port) if manager.options.webui.autoreload: # Create and destroy a socket so that any exceptions are raised before # we spawn a separate Python interpreter and lose this ability. from werkzeug.serving import run_with_reloader reloader_interval = 1 extra_files = None test_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) test_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) test_socket.bind( (manager.options.webui.bind, manager.options.webui.port)) test_socket.close() log.warning('Not starting scheduler, since autoreload is enabled.') run_with_reloader(start_server, extra_files, reloader_interval) else: # Start the scheduler manager.scheduler.start() start_server() log.debug('server exited') fire_event('webui.stop') manager.shutdown(finish_queue=False)
def start(mg): """Start WEB UI""" global manager manager = mg # Create sqlalchemy session for Flask usage global db_session db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=manager.engine)) if db_session is None: raise Exception('db_session is None') load_ui_plugins() # quick hack: since ui plugins may add tables to SQLAlchemy too and they're not initialized because create # was called when instantiating manager .. so we need to call it again from flexget.manager import Base Base.metadata.create_all(bind=manager.engine) app.register_blueprint(api) app.register_blueprint(api_schema) fire_event('webui.start') # Start Flask app.secret_key = os.urandom(24) set_exit_handler(stop_server) log.info('Starting server on port %s' % manager.options.webui.port) if manager.options.webui.autoreload: # Create and destroy a socket so that any exceptions are raised before # we spawn a separate Python interpreter and lose this ability. from werkzeug.serving import run_with_reloader reloader_interval = 1 extra_files = None test_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) test_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) test_socket.bind((manager.options.webui.bind, manager.options.webui.port)) test_socket.close() log.warning('Not starting scheduler, since autoreload is enabled.') run_with_reloader(start_server, extra_files, reloader_interval) else: # Start the scheduler manager.scheduler.start() start_server() log.debug('server exited') fire_event('webui.stop') manager.shutdown(finish_queue=False)
def main(): parser = ArgumentParser(description=__doc__, version='Mongo Task Queue (mtq) v%s' % mtq.__version__) parser.add_argument('queues', nargs='*', default=['default'], help='The queues to listen on (default: %(default)r)') parser.add_argument('-r', '--reloader', action='store_true', help='Reload the worker when it detects a change') parser.add_argument('-p', '--poll-interval', help='Sleep interval to check for jobs', default=3, type=int) parser.add_argument('-t', '--tags', nargs='*', help='only process jobs which contain all of the tags', default=[]) parser.add_argument('-c', '--config', help='Python module containing MTQ settings.') parser.add_argument( '-l', '--log-output', action='store_true', help='Store job and woker ouput in the db, seealso mtq-tail') parser.add_argument('-1', '--one', action='store_true', help='Process only the first job') parser.add_argument( '-b', '--batch', action='store_true', help='Process jobs until the queue is empty, then exit') parser.add_argument( '-j', '--job-id', type=object_id, help='Process the job (even if it has already been processed)') args = parser.parse_args() if args.reloader: from werkzeug.serving import run_with_reloader run_with_reloader(lambda: aux(args)) else: aux(args)
def run(self, host=None, port=None, debug=None, **options): from werkzeug.serving import make_server, run_with_reloader if host is None: host = '127.0.0.1' if port is None: server_name = self.config['SERVER_NAME'] if server_name and ':' in server_name: port = int(server_name.rsplit(':', 1)[1]) else: port = 5000 if debug is not None: self.debug = bool(debug) hostname = host port = port application = self use_reloader = self.debug use_debugger = self.debug if use_debugger: from werkzeug.debug import DebuggedApplication application = DebuggedApplication(application, True) try: from .webdav import dav_app except ImportError as e: logger.error('WebDav interface not enabled: %r', e) dav_app = None if dav_app: from werkzeug.wsgi import DispatcherMiddleware application = DispatcherMiddleware(application, { '/dav': dav_app }) def inner(): self.server = make_server(hostname, port, application) self.server.serve_forever() if os.environ.get('WERKZEUG_RUN_MAIN') != 'true': display_hostname = hostname != '*' and hostname or 'localhost' if ':' in display_hostname: display_hostname = '[%s]' % display_hostname self.logger.info('webui running on http://%s:%d/', display_hostname, port) if use_reloader: run_with_reloader(inner) else: inner()
def run(self): try: print 'Listening at %s port %d...' % (self.host, self.port) SocketIONamespace.set_app_class(self.app_class) if self.debug: self.flask_app = SocketIODebugger(self.flask_app, evalex=True, namespace=SocketIONamespace) server = SocketIOServer((self.host, self.port), self.flask_app, resource='socket.io', policy_server=False) if self.debug: run_with_reloader(server.serve_forever) else: server.serve_forever() except KeyboardInterrupt: print '\rShutting down...'
def run(self, host=None, port=None, debug=None, **options): from werkzeug.serving import make_server, run_with_reloader if host is None: host = '127.0.0.1' if port is None: server_name = self.config['SERVER_NAME'] if server_name and ':' in server_name: port = int(server_name.rsplit(':', 1)[1]) else: port = 5000 if debug is not None: self.debug = bool(debug) hostname = host port = port application = self use_reloader = self.debug use_debugger = self.debug if use_debugger: from werkzeug.debug import DebuggedApplication application = DebuggedApplication(application, True) try: from .webdav import dav_app except ImportError as e: logger.error('WebDav interface not enabled: %r', e) dav_app = None if dav_app: from werkzeug.wsgi import DispatcherMiddleware application = DispatcherMiddleware(application, {'/dav': dav_app}) def inner(): self.server = make_server(hostname, port, application) self.server.serve_forever() if os.environ.get('WERKZEUG_RUN_MAIN') != 'true': display_hostname = hostname != '*' and hostname or 'localhost' if ':' in display_hostname: display_hostname = '[%s]' % display_hostname self.logger.info('webui running on http://%s:%d/', display_hostname, port) if use_reloader: run_with_reloader(inner) else: inner()
def loop(threads, reload): # pylint: disable=redefined-builtin """ Run planned tasks and clean expired users, permanently. """ if reload is None: reload = current_app.debug def run_loop(): if reload: print("Running with reloader. Don't use this in production mode.") self.loop(int(threads), reload) if reload: from werkzeug.serving import run_with_reloader run_with_reloader(run_loop) else: run_loop()
def web_run(ctx, reloader): import os from werkzeug.serving import run_with_reloader from gevent import wsgi from api.app import rowboat, initialize os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' initialize(ctx.obj['config']) def run(): wsgi.WSGIServer(('0.0.0.0', 45000), rowboat.app).serve_forever() if reloader: run_with_reloader(run) else: run()
def run(self, app, host=None, port=None, **kwargs): if host is None: host = '127.0.0.1' if port is None: server_name = app.config['SERVER_NAME'] if server_name and ':' in server_name: port = int(server_name.rsplit(':', 1)[1]) else: port = 5000 #Don't allow override of resource, otherwise allow SocketIOServer kwargs to be passed through kwargs.pop('resource', None) self.server = SocketIOServer((host, port), app.wsgi_app, resource='socket.io', **kwargs) if app.debug: def run_server(): self.server.serve_forever() run_with_reloader(run_server) else: self.server.serve_forever()
def runServer(self): """Starts up the server. It (will) support different config options via the config plugin.""" config = pm.getService("config") debug = config.get("flask.debug") cFCGI = config.get("flask.fcgi") host = config.get("flask.bind") app_port = config.get("flask.app_port") fcgi_port = config.get("flask.fcgi_port") must_have_client_cert = config.get("flask.force_client_cert") if cFCGI: logger.info("registering fcgi server at %s:%i", host, fcgi_port) from flup.server.fcgi import WSGIServer WSGIServer(self._app, bindAddress=(host, fcgi_port)).run() else: logger.info("registering app server at %s:%i", host, app_port) # this workaround makes sure that the client cert can be acquired later (even when running the development server) # copied all this stuff from the actual flask implementation, so we can intervene and adjust the ssl context # self._app.run(host=host, port=app_port, ssl_context='adhoc', debug=debug, request_handler=ClientCertHTTPRequestHandler) # the code from flask's `run...` # see https://github.com/mitsuhiko/flask/blob/master/flask/app.py options = {} try: # now the code from werkzeug's `run_simple(host, app_port, self._app, **options)` # see https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/serving.py from werkzeug.debug import DebuggedApplication import socket application = DebuggedApplication(self._app, True) def inner(): server = serving.make_server(host, app_port, self._app, False, 1, ClientCertHTTPRequestHandler, False, 'adhoc') # The following line is the reason why I copied all that code! if must_have_client_cert: server.ssl_context.set_verify(SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT, lambda a,b,c,d,e: True) # That's it server.serve_forever() address_family = serving.select_ip_version(host, app_port) test_socket = socket.socket(address_family, socket.SOCK_STREAM) test_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) test_socket.bind((host, app_port)) test_socket.close() serving.run_with_reloader(inner, None, 1) finally: self._app._got_first_request = False
def run(info, host, port, reload, debugger, eager_loading): """Runs a local development server for the Flask-SocketIO application. The reloader and debugger are by default enabled if the debug flag of Flask is enabled and disabled otherwise. """ debug = get_debug_flag() if reload is None: reload = bool(debug) if debugger is None: debugger = bool(debug) if eager_loading is None: eager_loading = not reload # Extra startup messages. This depends a bit on Werkzeug internals to # not double execute when the reloader kicks in. if os.environ.get('WERKZEUG_RUN_MAIN') != 'true': # If we have an import path we can print it out now which can help # people understand what's being served. If we do not have an # import path because the app was loaded through a callback then # we won't print anything. if info.app_import_path is not None: print(' * Serving Flask-SocketIO app "%s"' % info.app_import_path) if debug is not None: print(' * Forcing debug mode %s' % (debug and 'on' or 'off')) def run_server(): app = info.load_app() if 'socketio' not in app.extensions: # flask-socketio is installed, but it isn't in this application # so we invoke Flask's original run command run_index = sys.argv.index('run') sys.argv = sys.argv[run_index:] return run_command() socketio = app.extensions['socketio'] socketio.run(app, host=host, port=port, debug=debugger, use_reloader=False, log_output=debugger) if reload: from werkzeug.serving import run_with_reloader run_with_reloader(run_server) else: run_server()
def main(prod, start_syncback, enable_tracer, config, port, enable_profiler): """ Launch the Nylas API service. """ level = os.environ.get("LOGLEVEL", inbox_config.get("LOGLEVEL")) configure_logging(log_level=level) maybe_enable_rollbar() if config is not None: config_path = os.path.abspath(config) load_overrides(config_path) if prod: start(port, start_syncback, enable_tracer, enable_profiler) else: preflight() from werkzeug.serving import run_with_reloader run_with_reloader(lambda: start(port, start_syncback, enable_tracer, enable_profiler))
def main(): parser = ArgumentParser(description=__doc__, version='Mongo Task Queue (mtq) v%s' % mtq.__version__, add_help=False) parser.add_argument('-c', '--config', help='Python module containing MTQ settings.') args, _ = parser.parse_known_args() config = config_dict(args.config) add_extra_arguments = config.get('extra_arguments') init_config = config.get('init') parser = ArgumentParser(description=__doc__, version='Mongo Task Queue (mtq) v%s' % mtq.__version__, add_help=True) parser.add_argument('-c', '--config', help='Python module containing MTQ settings.') parser.add_argument('queues', nargs='*', default=['default'], help='The queues to listen on (default: %(default)r)') parser.add_argument('-r', '--reloader', action='store_true', help='Reload the worker when it detects a change') parser.add_argument('-p', '--poll-interval', help='Sleep interval to check for jobs', default=3, type=int) parser.add_argument('-t', '--tags', nargs='*', help='only process jobs which contain all of the tags', default=[]) parser.add_argument('-l', '--log-output', action='store_true', help='Store job and woker ouput in the db, seealso mtq-tail') parser.add_argument('-1', '--one', action='store_true', help='Process only the first job') parser.add_argument('--backlog', '--list-jobs', action='store_true', help='List backlog of jobs and exit', dest='backlog') parser.add_argument('-b', '--batch', action='store_true', help='Process jobs until the queue is empty, then exit') parser.add_argument('-j', '--job-id', type=object_id, help='Process the job (even if it has already been processed)') parser.add_argument('-f', '--failed', action='store_true', help='Process failed jobs') if add_extra_arguments: add_extra_arguments(parser) args = parser.parse_args() if init_config: init_config(args) if args.reloader: from werkzeug.serving import run_with_reloader run_with_reloader(lambda: aux(args)) else: aux(args)
def main(prod, config, process_num, syncback_id, enable_tracer, enable_profiler): """ Launch the actions syncback service. """ setproctitle("syncback-{}".format(process_num)) maybe_enable_rollbar() print("Python", sys.version, file=sys.stderr) if config is not None: config_path = os.path.abspath(config) load_overrides(config_path) level = os.environ.get("LOGLEVEL", inbox_config.get("LOGLEVEL")) configure_logging(log_level=level) reconfigure_logging() total_processes = int(os.environ.get("SYNCBACK_PROCESSES", 1)) def start(): # Start the syncback service, and just hang out forever syncback = SyncbackService(syncback_id, process_num, total_processes) if enable_profiler: inbox_config["DEBUG_PROFILING_ON"] = True port = 16384 + process_num enable_profiler_api = inbox_config.get("DEBUG_PROFILING_ON") frontend = SyncbackHTTPFrontend(port, enable_tracer, enable_profiler_api) frontend.start() syncback.start() syncback.join() if prod: start() else: preflight() from werkzeug.serving import run_with_reloader run_with_reloader(start)
def run(info, host, port, reload, debugger, eager_loading): """Runs a local development server for the Flask-SocketIO application. The reloader and debugger are by default enabled if the debug flag of Flask is enabled and disabled otherwise. """ debug = get_debug_flag() if reload is None: reload = bool(debug) if debugger is None: debugger = bool(debug) if eager_loading is None: eager_loading = not reload # Extra startup messages. This depends a bit on Werkzeug internals to # not double execute when the reloader kicks in. if os.environ.get('WERKZEUG_RUN_MAIN') != 'true': # If we have an import path we can print it out now which can help # people understand what's being served. If we do not have an # import path because the app was loaded through a callback then # we won't print anything. if info.app_import_path is not None: print(' * Serving Flask-SocketIO app "%s"' % info.app_import_path) if debug is not None: print(' * Forcing debug mode %s' % (debug and 'on' or 'off')) def run_server(): app = info.load_app() socketio = app.extensions['socketio'] socketio.run(app, host=host, port=port, debug=debugger, use_reloader=False, log_output=debugger) if reload: from werkzeug.serving import run_with_reloader run_with_reloader(run_server) else: run_server()
def gevent_run(app): from gevent.wsgi import WSGIServer import gevent.monkey from werkzeug.debug import DebuggedApplication gevent.monkey.patch_socket() run_app = app if app.config['DEBUG']: run_app = DebuggedApplication(app) def run_server(): port = int(app.config.get('PORT', 5000)) address = app.config.get('ADDRESS', '') print('Listening on http://%s:%d/' % (address or '0.0.0.0', port)) http_server = WSGIServer((address, port), run_app) http_server.serve_forever() if app.config['DEBUG']: from werkzeug.serving import run_with_reloader run_with_reloader(run_server) else: run_server()
def run(self, app, host='localhost', port=5000, reload=True): def _run_server(): if io.async_mode == 'threading': # deploy with Werkzeug app.run(threaded=True) elif io.async_mode == 'eventlet': # deploy with eventlet import eventlet import eventlet.wsgi eventlet.wsgi.server(eventlet.listen((host, port)), app) elif io.async_mode == 'gevent': # deploy with gevent from gevent import pywsgi try: from geventwebsocket.handler import WebSocketHandler websocket = True except ImportError: websocket = False if websocket: pywsgi.WSGIServer( (host, port), app, handler_class=WebSocketHandler).serve_forever() else: pywsgi.WSGIServer((host, port), app).serve_forever() elif io.async_mode == 'gevent_uwsgi': print( 'Start the application through the uwsgi server. Example:') print( 'uwsgi --http :5000 --gevent 1000 --http-websockets --master --wsgi-file app.py --callable app' ) else: print('Unknown async_mode: ' + io.async_mode) app.wsgi_app = _io.Middleware(io, app.wsgi_app) if reload: from werkzeug.serving import run_with_reloader run_with_reloader(_run_server) else: _run_server()
def run(self, host=None, port=None, debug=None, **options): from werkzeug.serving import make_server, run_with_reloader if host is None: host = '127.0.0.1' if port is None: server_name = self.config['SERVER_NAME'] if server_name and ':' in server_name: port = int(server_name.rsplit(':', 1)[1]) else: port = 5000 if debug is not None: self.debug = bool(debug) #run_simple(host, port, self, **options) hostname = host port = port application = self use_reloader = self.debug use_debugger = self.debug if use_debugger: from werkzeug.debug import DebuggedApplication application = DebuggedApplication(application, True) def inner(): self.server = make_server(hostname, port, application) self.server.serve_forever() if os.environ.get('WERKZEUG_RUN_MAIN') != 'true': display_hostname = hostname != '*' and hostname or 'localhost' if ':' in display_hostname: display_hostname = '[%s]' % display_hostname self.logger.info('webui running on http://%s:%d/', display_hostname, port) if use_reloader: run_with_reloader(inner) else: inner()
def run_server(hostname, port, application, use_reloader=False, reloader_interval=1, static_files=None, request_handler=WebSocketHandler): """"运行服务器""" import werkzeug from werkzeug._internal import _log from werkzeug.serving import select_ip_version, socket, run_with_reloader if static_files: from werkzeug.wsgi import SharedDataMiddleware application = SharedDataMiddleware(application, static_files) def inner(): make_server(hostname, port, application, request_handler).serve_forever() if os.environ.get('WERKZEUG_RUN_MAIN') != 'true': display_hostname = hostname != '*' and hostname or 'localhost' if ':' in display_hostname: display_hostname = '[%s]' % display_hostname _log('info', ' * Running on http://%s:%d/', display_hostname, port) if request_handler and hasattr(request_handler, 'run_websocket'): _log('info', ' * Running on ws://%s:%d/', display_hostname, port) if use_reloader: address_family = select_ip_version(hostname, port) test_socket = socket.socket(address_family, socket.SOCK_STREAM) test_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) test_socket.bind((hostname, port)) test_socket.close() run_with_reloader(inner, None, reloader_interval) else: inner()
def run(self, app, host=None, port=None, **kwargs): if host is None: host = '127.0.0.1' if port is None: server_name = app.config['SERVER_NAME'] if server_name and ':' in server_name: port = int(server_name.rsplit(':', 1)[1]) else: port = 5000 resource = kwargs.pop('resource', 'socket.io') self.server = SocketIOServer((host, port), app.wsgi_app, resource=resource, **kwargs) if app.debug: # monkey patching is required by the reloader from gevent import monkey monkey.patch_all() def run_server(): self.server.serve_forever() if os.environ.get('WERKZEUG_RUN_MAIN') != 'true': _log('info', ' * Running on http://%s:%d/' % (host, port)) run_with_reloader(run_server) else: self.server.serve_forever()
def handle(self, app, host, port, profile, profile_max): """Runs a development server.""" if profile: wsgi = ProfilerMiddleware(app, restrictions=[profile_max]) elif self.use_debugger: wsgi = DebuggedApplication(app) else: wsgi = app def run(): print('Start server at: {0}:{1}'.format(host, port)) http_server = WSGIServer((host, port), wsgi, **self.server_options) http_server.serve_forever() if self.use_reloader: run = run_with_reloader(run) run()
def run_currentapp(logger_names): def show_loggers(logger_names): import logging from logging import handlers for logger_name in logger_names: logger = logging.getLogger(logger_name) logger.setLevel(logging.DEBUG) #logger.addHandler( #logging.StreamHandler() #) show_loggers(logger_names) from werkzeug.serving import run_with_reloader from gevent import monkey; monkey.patch_all() from socketio.server import SocketIOServer from wdb import Wdb app = config_app(_get_current_app()) app.debug = True PORT = 5000 def _(): SocketIOServer(('', PORT), app, resource="socket.io").serve_forever() return run_with_reloader(_, interval=1)
def run(self, app, host=None, port=None, **kwargs): """Run the SocketIO web server. :param app: The Flask application instance. :param host: The hostname or IP address for the server to listen on. Defaults to 127.0.0.1. :param port: The port number for the server to listen on. Defaults to 5000. :param debug: ``True`` to start the server in debug mode, ``False`` to start in normal mode. :param use_reloader: ``True`` to enable the Flask reloader, ``False`` to disable it. :param extra_files: A list of additional files that the Flask reloader should watch. Defaults to ``None`` :param log_output: If ``True``, the server logs all incomming connections. If ``False`` logging is disabled. Defaults to ``True`` in debug mode, ``False`` in normal mode. Unused when the threading async mode is used. :param kwargs: Additional web server options. The web server options are specific to the server used in each of the supported async modes. Note that options provided here will not be seen when using an external web server such as gunicorn, since this method is not called in that case. """ if host is None: host = '127.0.0.1' if port is None: server_name = app.config['SERVER_NAME'] if server_name and ':' in server_name: port = int(server_name.rsplit(':', 1)[1]) else: port = 5000 debug = kwargs.pop('debug', app.debug) log_output = kwargs.pop('log_output', debug) use_reloader = kwargs.pop('use_reloader', debug) extra_files = kwargs.pop('extra_files', None) app.debug = debug if app.debug and self.server.eio.async_mode != 'threading': # put the debug middleware between the SocketIO middleware # and the Flask application instance # # mw1 mw2 mw3 Flask app # o ---- o ---- o ---- o # / # o Flask-SocketIO # \ middleware # o # Flask-SocketIO WebSocket handler # # BECOMES # # dbg-mw mw1 mw2 mw3 Flask app # o ---- o ---- o ---- o ---- o # / # o Flask-SocketIO # \ middleware # o # Flask-SocketIO WebSocket handler # self.sockio_mw.wsgi_app = DebuggedApplication( self.sockio_mw.wsgi_app, evalex=True) if self.server.eio.async_mode == 'threading': from werkzeug._internal import _log _log( 'warning', 'WebSocket transport not available. Install ' 'eventlet or gevent and gevent-websocket for ' 'improved performance.') app.run(host=host, port=port, threaded=True, use_reloader=use_reloader, **kwargs) elif self.server.eio.async_mode == 'eventlet': def run_server(): import eventlet eventlet_socket = eventlet.listen((host, port)) # If provided an SSL argument, use an SSL socket ssl_args = [ 'keyfile', 'certfile', 'server_side', 'cert_reqs', 'ssl_version', 'ca_certs', 'do_handshake_on_connect', 'suppress_ragged_eofs', 'ciphers' ] ssl_params = {k: kwargs[k] for k in kwargs if k in ssl_args} if len(ssl_params) > 0: for k in ssl_params: kwargs.pop(k) ssl_params['server_side'] = True # Listening requires true eventlet_socket = eventlet.wrap_ssl( eventlet_socket, **ssl_params) eventlet.wsgi.server(eventlet_socket, app, log_output=log_output, **kwargs) if use_reloader: run_with_reloader(run_server, extra_files=extra_files) else: run_server() elif self.server.eio.async_mode == 'gevent': from gevent import pywsgi try: from geventwebsocket.handler import WebSocketHandler websocket = True except ImportError: websocket = False log = 'default' if not log_output: log = None if websocket: self.wsgi_server = pywsgi.WSGIServer( (host, port), app, handler_class=WebSocketHandler, log=log, **kwargs) else: self.wsgi_server = pywsgi.WSGIServer((host, port), app, log=log) if use_reloader: # monkey patching is required by the reloader from gevent import monkey monkey.patch_all() def run_server(): self.wsgi_server.serve_forever() run_with_reloader(run_server, extra_files=extra_files) else: self.wsgi_server.serve_forever()
@app.route("/404") def error_404(): return render_template("404.html") @app.route('/download/cv', methods=["GET"]) def download_cv(): return send_from_directory(directory="static/downloads", filename="cv.pdf") @app.route('/download/vc', methods=["GET"]) def download_vc(): return send_from_directory(directory="static/downloads", filename="vc.pdf") # SERVER def run_server(): if app.debug: application = DebuggedApplication(app) else: application = app server = pywsgi.WSGIServer(("", 80), application, handler_class=WebSocketHandler) server.serve_forever() if __name__ == "__main__": run_with_reloader(run_server)
def run(self, app, host=None, port=None, **kwargs): """Run the SocketIO web server. :param app: The Flask application instance. :param host: The hostname or IP address for the server to listen on. Defaults to 127.0.0.1. :param port: The port number for the server to listen on. Defaults to 5000. :param debug: ``True`` to start the server in debug mode, ``False`` to start in normal mode. :param use_reloader: ``True`` to enable the Flask reloader, ``False`` to disable it. :param extra_files: A list of additional files that the Flask reloader should watch. Defaults to ``None`` :param log_output: If ``True``, the server logs all incomming connections. If ``False`` logging is disabled. Defaults to ``True`` in debug mode, ``False`` in normal mode. Unused wheb the threading async mode is used. :param kwargs: Additional web server, Socket.IO, Engine.IO options. The web server options are specific to the server used in each of the supported async modes. See the constructor of this class for the list of Socket.IO and Engine.IO options. Note that options provided here will not be available when using an external web server such as gunicorn, since this method is not called in that case. """ if host is None: host = '127.0.0.1' if port is None: server_name = app.config['SERVER_NAME'] if server_name and ':' in server_name: port = int(server_name.rsplit(':', 1)[1]) else: port = 5000 debug = kwargs.pop('debug', app.debug) log_output = kwargs.pop('log_output', debug) use_reloader = kwargs.pop('use_reloader', debug) extra_files = kwargs.pop('extra_files', None) self.server_options.update(kwargs) app.debug = debug if app.debug and self.server.eio.async_mode != 'threading': # app.wsgi_app is the _SocketIOMiddleware instance, set in the # constructor. # app.wsgi_app.wsgi_app is the wsgi_app from the Flask application # instance flask_app = app.wsgi_app flask_app.wsgi_app = DebuggedApplication(flask_app.wsgi_app, evalex=True) if self.server.eio.async_mode == 'threading': from werkzeug._internal import _log _log('warning', 'WebSocket transport not available. Install ' 'eventlet or gevent and gevent-websocket for ' 'improved performance.') app.run(host=host, port=port, threaded=True, use_reloader=use_reloader, **kwargs) elif self.server.eio.async_mode == 'eventlet': def run_server(): import eventlet eventlet.wsgi.server(eventlet.listen((host, port)), app, log_output=log_output, **kwargs) if use_reloader: run_with_reloader(run_server, extra_files=extra_files) else: run_server() elif self.server.eio.async_mode == 'gevent': from gevent import pywsgi try: from geventwebsocket.handler import WebSocketHandler websocket = True except ImportError: websocket = False log = 'default' if not log_output: log = None if websocket: self.wsgi_server = pywsgi.WSGIServer( (host, port), app, handler_class=WebSocketHandler, log=log) else: self.wsgi_server = pywsgi.WSGIServer((host, port), app, log=log) if use_reloader: # monkey patching is required by the reloader from gevent import monkey monkey.patch_all() def run_server(): self.wsgi_server.serve_forever() run_with_reloader(run_server, extra_files=extra_files) else: self.wsgi_server.serve_forever()
) if event_notifier: event_notifiers += ( Launcher.ConditionNotifier(event_notifier),) launcher = Launcher.Launcher( '.', 'main_machine', 'main_instance', waiters, "logging.configuration.txt", before_shutdown, event_notifiers ) launcher._wait_condition = condition return launcher def launch(signals = False): launcher = inner(signals = signals) launcher.launch() return launcher if __name__ == '__main__': try: from werkzeug.serving import run_with_reloader except ImportError: print("werkzeug.serving not installed (pip install werkzeug). If you're developing, you'll have to restart the application in every change manually.") launch(signals = True) else: run_with_reloader(launch)
def runServer(self, services=[]): """Starts up the server. It (will) support different config options via the config plugin.""" self.add_routes() debug = self.general_section.get("debug") host = self.general_section.get("host") app_port = int(self.general_section.get("port")) template_folder = self.general_section.get("template_folder") cFCGI = ast.literal_eval(self.fcgi_section.get("enabled")) fcgi_port = int(self.fcgi_section.get("port")) must_have_client_cert = ast.literal_eval(self.certificates_section.get("force_client_certificate")) if cFCGI: logger.info("registering fcgi server at %s:%i", host, fcgi_port) from flup.server.fcgi import WSGIServer WSGIServer(self._app, bindAddress=(host, fcgi_port)).run() else: logger.info("registering app server at %s:%i", host, app_port) # this workaround makes sure that the client cert can be acquired later (even when running the development server) # copied all this stuff from the actual flask implementation, so we can intervene and adjust the ssl context # self._app.run(host=host, port=app_port, ssl_context='adhoc', debug=debug, request_handler=ClientCertHTTPRequestHandler) # the code from flask's `run...` # see https://github.com/mitsuhiko/flask/blob/master/flask/app.py options = {} try: # now the code from werkzeug's `run_simple(host, app_port, self._app, **options)` # see https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/serving.py from werkzeug.debug import DebuggedApplication import socket application = DebuggedApplication(self._app, True) # Set up an SSL context from OpenSSL import SSL context = SSL.Context(SSL.SSLv23_METHOD) certs_path = os.path.normpath(os.path.join(os.path.dirname(__file__), "../../..", "cert")) context_crt = os.path.join(certs_path, "server.crt") context_key = os.path.join(certs_path, "server.key") try: context.use_certificate_file(context_crt) context.use_privatekey_file(context_key) except Exception as e: logger.critical("error starting flask server. Cert or key is missing under %s", certs_path) sys.exit(e) def inner(): #server = serving.make_server(host, app_port, self._app, False, 1, ClientCertHTTPRequestHandler, False, 'adhoc') server = serving.make_server(host, app_port, self._app, False, 1, ClientCertHTTPRequestHandler, False, ssl_context=context) #server = serving.make_server(host, app_port, self._app, False, 1, ClientCertHTTPRequestHandler, False, ssl_context=(context_crt, context_key)) # The following line is the reason why I copied all that code! if must_have_client_cert: # FIXME: what works with web app does not work with cli. Check this out server.ssl_context.set_verify(SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT, lambda a,b,c,d,e: True) # before enter in the loop, start the supplementary services for s in services: s.start() # That's it server.serve_forever() address_family = serving.select_ip_version(host, app_port) test_socket = socket.socket(address_family, socket.SOCK_STREAM) test_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) test_socket.bind((host, app_port)) test_socket.close() serving.run_with_reloader(inner, None, 1) finally: self._app._got_first_request = False
def main(): sys.path.append(os.curdir) parser = argparse.ArgumentParser(description='Run some watchers.') parser.add_argument('--port', type=int, default=8080) parser.add_argument('--address-family', type=str, default='AF_INET', choices=sorted(_ADDRESS_FAMILY.keys())) parser.add_argument('--socket-type', type=str, default='SOCK_STREAM', choices=sorted(_SOCKET_TYPE.keys())) group = parser.add_mutually_exclusive_group() group.add_argument('--host', default='localhost') group.add_argument('--fd', type=int, default=-1) group.add_argument('--backlog', type=int, default=2048) parser.add_argument('--backend', type=str, default='wsgiref', choices=backends()) parser.add_argument('--use-reloader', action='store_true', help="Restart server when source files change") parser.add_argument('--spawn', type=int, default=None, help="Spawn type, only makes sense if the backend " "supports it (gevent)") parser.add_argument('--graceful-timeout', type=int, default=None, help="Graceful shutdown timeout for existing requests " "to complete, only for backends that support it " "(%s)" % ', '.join(_SUPPORTS_GRACEFUL_TIMEOUT)) parser.add_argument('application', default='chaussette.util.hello_app', nargs='?') parser.add_argument('arguments', default=[], nargs='*', help="Optional arguments you may need for your app") parser.add_argument('--pre-hook', type=str, default=None) parser.add_argument('--post-hook', type=str, default=None) parser.add_argument('--python-path', type=str, default=None) log_levels = itertools.chain.from_iterable((key.upper(), key) for key in LOG_LEVELS.keys()) parser.add_argument('--log-level', dest='loglevel', default='info', choices=sorted(log_levels), help="log level") parser.add_argument('--log-output', dest='logoutput', default='-', help="log output") args = parser.parse_args() if is_gevent_backend(args.backend): from gevent import monkey monkey.noisy = False monkey.patch_all() application = args.application logger = chaussette_logger configure_logger(logger, args.loglevel, args.logoutput) if args.graceful_timeout is not None and \ args.backend not in _SUPPORTS_GRACEFUL_TIMEOUT: logger.info('Sorry %r does not support --graceful_timeout' % args.backend) sys.exit(0) if application.startswith('paste:'): from chaussette._paste import paste_app app = paste_app(application.split(':')[-1]) else: app = import_string(application) if args.fd != -1: host = 'fd://%d' % args.fd else: host = args.host # pre-hook ? if args.pre_hook is not None: pre_hook = import_string(args.pre_hook) logger.info('Running the pre-hook %r' % pre_hook) pre_hook(args) # post-hook ? if args.post_hook is not None: post_hook = import_string(args.post_hook) else: post_hook = None address_family = _ADDRESS_FAMILY[args.address_family] if address_family == socket.AF_UNIX and args.backend in _NO_UNIX: logger.info('Sorry %r does not support unix sockets' % args.backend) sys.exit(0) def inner(): try: httpd = make_server(app, host=host, port=args.port, backend=args.backend, backlog=args.backlog, spawn=args.spawn, graceful_timeout=args.graceful_timeout, logger=logger, address_family=address_family, socket_type=_SOCKET_TYPE[args.socket_type]) try: httpd.serve_forever() except KeyboardInterrupt: sys.exit(0) finally: if post_hook is not None: logger.info('Running the post-hook %r' % post_hook) post_hook(args) if args.use_reloader: try: from werkzeug.serving import run_with_reloader except ImportError: logger.info("Reloader requires Werkzeug: " "'pip install werkzeug'") sys.exit(0) run_with_reloader(inner) else: inner()
def run(self, app, host=None, port=None, **kwargs): """Run the SocketIO web server. :param app: The Flask application instance. :param host: The hostname or IP address for the server to listen on. Defaults to 127.0.0.1. :param port: The port number for the server to listen on. Defaults to 5000. :param debug: ``True`` to start the server in debug mode, ``False`` to start in normal mode. :param use_reloader: ``True`` to enable the Flask reloader, ``False`` to disable it. :param extra_files: A list of additional files that the Flask reloader should watch. Defaults to ``None`` :param log_output: If ``True``, the server logs all incomming connections. If ``False`` logging is disabled. Defaults to ``True`` in debug mode, ``False`` in normal mode. Unused when the threading async mode is used. :param kwargs: Additional web server options. The web server options are specific to the server used in each of the supported async modes. Note that options provided here will not be seen when using an external web server such as gunicorn, since this method is not called in that case. """ if host is None: host = "127.0.0.1" if port is None: server_name = app.config["SERVER_NAME"] if server_name and ":" in server_name: port = int(server_name.rsplit(":", 1)[1]) else: port = 5000 debug = kwargs.pop("debug", app.debug) log_output = kwargs.pop("log_output", debug) use_reloader = kwargs.pop("use_reloader", debug) extra_files = kwargs.pop("extra_files", None) app.debug = debug if app.debug and self.server.eio.async_mode != "threading": # app.wsgi_app is the _SocketIOMiddleware instance, set in the # constructor. # app.wsgi_app.wsgi_app is the wsgi_app from the Flask application # instance flask_app = app.wsgi_app flask_app.wsgi_app = DebuggedApplication(flask_app.wsgi_app, evalex=True) if self.server.eio.async_mode == "threading": from werkzeug._internal import _log _log( "warning", "WebSocket transport not available. Install " "eventlet or gevent and gevent-websocket for " "improved performance.", ) app.run(host=host, port=port, threaded=True, use_reloader=use_reloader, **kwargs) elif self.server.eio.async_mode == "eventlet": def run_server(): import eventlet eventlet_socket = eventlet.listen((host, port)) # If provided an SSL argument, use an SSL socket ssl_args = [ "keyfile", "certfile", "server_side", "cert_reqs", "ssl_version", "ca_certs", "do_handshake_on_connect", "suppress_ragged_eofs", "ciphers", ] ssl_params = {k: kwargs[k] for k in kwargs if k in ssl_args} if len(ssl_params) > 0: for k in ssl_params: kwargs.pop(k) ssl_params["server_side"] = True # Listening requires true eventlet_socket = eventlet.wrap_ssl(eventlet_socket, **ssl_params) eventlet.wsgi.server(eventlet_socket, app, log_output=log_output, **kwargs) if use_reloader: run_with_reloader(run_server, extra_files=extra_files) else: run_server() elif self.server.eio.async_mode == "gevent": from gevent import pywsgi try: from geventwebsocket.handler import WebSocketHandler websocket = True except ImportError: websocket = False log = "default" if not log_output: log = None if websocket: self.wsgi_server = pywsgi.WSGIServer( (host, port), app, handler_class=WebSocketHandler, log=log, **kwargs ) else: self.wsgi_server = pywsgi.WSGIServer((host, port), app, log=log) if use_reloader: # monkey patching is required by the reloader from gevent import monkey monkey.patch_all() def run_server(): self.wsgi_server.serve_forever() run_with_reloader(run_server, extra_files=extra_files) else: self.wsgi_server.serve_forever()
def run(self, app, host=None, port=None, **kwargs): """Run the SocketIO web server. :param app: The Flask application instance. :param host: The hostname or IP address for the server to listen on. Defaults to 127.0.0.1. :param port: The port number for the server to listen on. Defaults to 5000. :param use_reloader: ``True`` to enable the Flask reloader, ``False`` to disable it. :param extra_files: A list of additional files that the Flask reloader should observe. Defaults to ``None`` :param resource: The SocketIO resource name. Defaults to ``'socket.io'``. Leave this as is unless you know what you are doing. :param transports: Optional list of transports to allow. List of strings, each string should be one of handler.SocketIOHandler.handler_types. :param policy_server: Boolean describing whether or not to use the Flash policy server. Defaults to ``True``. :param policy_listener: A tuple containing (host, port) for the policy server. This is optional and used only if policy server is set to true. Defaults to 0.0.0.0:843. :param heartbeat_interval: The timeout for the server, we should receive a heartbeat from the client within this interval. This should be less than the ``heartbeat_timeout``. :param heartbeat_timeout: The timeout for the client when it should send a new heartbeat to the server. This value is sent to the client after a successful handshake. :param close_timeout: The timeout for the client, when it closes the connection it still X amounts of seconds to do re-open of the connection. This value is sent to the client after a successful handshake. :param log_file: The file in which you want the PyWSGI server to write its access log. If not specified, it is sent to ``stderr`` (with gevent 0.13). """ if host is None: host = '127.0.0.1' if port is None: server_name = app.config['SERVER_NAME'] if server_name and ':' in server_name: port = int(server_name.rsplit(':', 1)[1]) else: port = 5000 resource = kwargs.pop('resource', 'socket.io') use_reloader = kwargs.pop('use_reloader', app.debug) extra_files = kwargs.pop('extra_files', None) self.server = SocketIOServer((host, port), app.wsgi_app, resource=resource, **kwargs) if use_reloader: # monkey patching is required by the reloader from gevent import monkey monkey.patch_all() def run_server(): self.server.serve_forever() if os.environ.get('WERKZEUG_RUN_MAIN') != 'true': _log('info', ' * Running on http://%s:%d/' % (host, port)) run_with_reloader(run_server, extra_files=extra_files) else: _log('info', ' * Running on http://%s:%d/' % (host, port)) self.server.serve_forever()
def start(mg): """Start WEB UI""" global manager manager = mg # Create sqlalchemy session for Flask usage global db_session db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=manager.engine)) if db_session is None: raise Exception('db_session is None') if os.name != 'nt' and manager.options.daemon: if threading.activeCount() != 1: log.critical('There are %r active threads. ' 'Daemonizing now may cause strange failures.' % threading.enumerate()) log.info('Creating FlexGet Daemon.') newpid = daemonize() # Write new pid to lock file log.debug('Writing new pid %d to lock file %s' % (newpid, manager.lockfile)) lockfile = file(manager.lockfile, 'w') try: lockfile.write('%d\n' % newpid) finally: lockfile.close() # Start the executor thread global executor executor = ExecThread() executor.start() # Initialize manager manager.create_feeds() load_ui_plugins() # quick hack: since ui plugins may add tables to SQLAlchemy too and they're not initialized because create # was called when instantiating manager .. so we need to call it again from flexget.manager import Base Base.metadata.create_all(bind=manager.engine) fire_event('webui.start') # Start Flask app.secret_key = os.urandom(24) set_exit_handler(stop_server) log.info('Starting server on port %s' % manager.options.port) if manager.options.autoreload: # Create and destroy a socket so that any exceptions are raised before # we spawn a separate Python interpreter and lose this ability. import socket from werkzeug.serving import run_with_reloader reloader_interval = 1 extra_files = None test_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) test_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) test_socket.bind(('0.0.0.0', manager.options.port)) test_socket.close() run_with_reloader(start_server, extra_files, reloader_interval) else: start_server() log.debug('server exited') fire_event('webui.stop') manager.shutdown()
def run(self, app, host=None, port=None, **kwargs): """Run the SocketIO web server. :param app: The Flask application instance. :param host: The hostname or IP address for the server to listen on. Defaults to 127.0.0.1. :param port: The port number for the server to listen on. Defaults to 5000. :param debug: ``True`` to start the server in debug mode, ``False`` to start in normal mode. :param use_reloader: ``True`` to enable the Flask reloader, ``False`` to disable it. :param extra_files: A list of additional files that the Flask reloader should watch. Defaults to ``None`` :param log_output: If ``True``, the server logs all incomming connections. If ``False`` logging is disabled. Defaults to ``True`` in debug mode, ``False`` in normal mode. Unused when the threading async mode is used. :param kwargs: Additional web server options. The web server options are specific to the server used in each of the supported async modes. Note that options provided here will not be seen when using an external web server such as gunicorn, since this method is not called in that case. """ if host is None: host = '127.0.0.1' if port is None: server_name = app.config['SERVER_NAME'] if server_name and ':' in server_name: port = int(server_name.rsplit(':', 1)[1]) else: port = 5000 debug = kwargs.pop('debug', app.debug) log_output = kwargs.pop('log_output', debug) use_reloader = kwargs.pop('use_reloader', debug) extra_files = kwargs.pop('extra_files', None) app.debug = debug if app.debug and self.server.eio.async_mode != 'threading': # put the debug middleware between the SocketIO middleware # and the Flask application instance # # mw1 mw2 mw3 Flask app # o ---- o ---- o ---- o # / # o Flask-SocketIO # \ middleware # o # Flask-SocketIO WebSocket handler # # BECOMES # # dbg-mw mw1 mw2 mw3 Flask app # o ---- o ---- o ---- o ---- o # / # o Flask-SocketIO # \ middleware # o # Flask-SocketIO WebSocket handler # self.sockio_mw.wsgi_app = DebuggedApplication(self.sockio_mw.wsgi_app, evalex=True) if self.server.eio.async_mode == 'threading': from werkzeug._internal import _log _log('warning', 'WebSocket transport not available. Install ' 'eventlet or gevent and gevent-websocket for ' 'improved performance.') app.run(host=host, port=port, threaded=True, use_reloader=use_reloader, **kwargs) elif self.server.eio.async_mode == 'eventlet': def run_server(): import eventlet import eventlet.wsgi eventlet_socket = eventlet.listen((host, port)) # If provided an SSL argument, use an SSL socket ssl_args = ['keyfile', 'certfile', 'server_side', 'cert_reqs', 'ssl_version', 'ca_certs', 'do_handshake_on_connect', 'suppress_ragged_eofs', 'ciphers'] ssl_params = {k: kwargs[k] for k in kwargs if k in ssl_args} if len(ssl_params) > 0: for k in ssl_params: kwargs.pop(k) ssl_params['server_side'] = True # Listening requires true eventlet_socket = eventlet.wrap_ssl(eventlet_socket, **ssl_params) eventlet.wsgi.server(eventlet_socket, app, log_output=log_output, **kwargs) if use_reloader: run_with_reloader(run_server, extra_files=extra_files) else: run_server() elif self.server.eio.async_mode == 'gevent': from gevent import pywsgi try: from geventwebsocket.handler import WebSocketHandler websocket = True except ImportError: websocket = False log = 'default' if not log_output: log = None if websocket: self.wsgi_server = pywsgi.WSGIServer( (host, port), app, handler_class=WebSocketHandler, log=log, **kwargs) else: self.wsgi_server = pywsgi.WSGIServer((host, port), app, log=log) if use_reloader: # monkey patching is required by the reloader from gevent import monkey monkey.patch_all() def run_server(): self.wsgi_server.serve_forever() run_with_reloader(run_server, extra_files=extra_files) else: self.wsgi_server.serve_forever()