def initialize(self, *, path=None, sock=None, shutdown_timeout=60, ssl_context=None, backlog=128, access_log_class=AccessLogger, access_log_format=AccessLogger.LOG_FORMAT, access_log=AccessLogger.access_logger, handle_signals=True, reuse_address=None, reuse_port=None): # 初始化模板 self._initialize_template() # 初始化session self._initialize_session() # 初始化csrf self._initialize_csrf() self._initialize_runner(handle_signals, access_log_class, access_log_format, access_log) sites = [] try: sites.append(web.TCPSite(runner, self.app.config["APPS"][self.app_name]['host'], self.app.config["APPS"][ self.app_name]['port'], shutdown_timeout=shutdown_timeout, ssl_context=ssl_context, backlog=backlog, reuse_address=reuse_address, reuse_port=reuse_port)) if path is not None: if isinstance(path, (str, bytes, bytearray, memoryview)): sites.append(web.UnixSite(runner, path, shutdown_timeout=shutdown_timeout, ssl_context=ssl_context, backlog=backlog)) else: for p in path: sites.append(web.UnixSite(runner, p, shutdown_timeout=shutdown_timeout, ssl_context=ssl_context, backlog=backlog)) if sock is not None: if not isinstance(sock, Iterable): sites.append(web.SockSite(runner, sock, shutdown_timeout=shutdown_timeout, ssl_context=ssl_context, backlog=backlog)) else: for s in sock: sites.append(web.SockSite(runner, s, shutdown_timeout=shutdown_timeout, ssl_context=ssl_context, backlog=backlog)) for site in sites: self.loop.run_until_complete(site.start()) finally: self.loop.run_until_complete(runner.cleanup()) self.show_info()
async def _run(self): ctx = self._create_ssl_context(self.cfg) if self.cfg.is_ssl else None for sock in self.sockets: site = web.SockSite(self._runner, sock, ssl_context=ctx, shutdown_timeout=self.cfg.graceful_timeout / 100 * 95) await site.start() # If our parent changed then we shut down. pid = os.getpid() try: while self.alive: self.notify() cnt = self._runner.server.requests_count if self.cfg.max_requests and cnt > self.cfg.max_requests: self.alive = False self.log.info("Max requests, shutting down: %s", self) elif pid == os.getpid() and self.ppid != os.getppid(): self.alive = False self.log.info("Parent changed, shutting down: %s", self) else: await self._wait_next_notify() except BaseException: pass await self._runner.cleanup()
async def link_httpserver(self): ws_port = await self.web_port_chan.recv() # init http server handlers ins_url = self._api._ins_url md_url = self._api._md_url ws_url = 'ws://127.0.0.1:' + str(ws_port['port']) app = web.Application() app.router.add_get(path='/url', handler=lambda request: TqWebHelper. httpserver_url_handler(ins_url, md_url, ws_url)) app.router.add_get(path='/', handler=lambda request: TqWebHelper. httpserver_index_handler(self._web_dir)) app.router.add_static('/', self._web_dir, show_index=True) runner = web.AppRunner(app) await runner.setup() server_socket = socket.socket() server_socket.bind(('127.0.0.1', self._http_server_port)) address = server_socket.getsockname() site = web.SockSite(runner, server_socket) await site.start() self._logger.info("您可以访问 http://{ip}:{port} 查看策略绘制出的 K 线图形。".format( ip=address[0], port=address[1])) await asyncio.sleep(100000000000)
async def serve(app): runner = web.AppRunner(app) await runner.setup() try: # Try and open up the unix socket passed to us from systemd site = web.SockSite( runner, socket.fromfd(3, socket.AF_UNIX, socket.SOCK_STREAM)) await site.start() logging.info("Using systemd socket activation") except: # We must be running in some other setup - bind the socket ourselves site = web.UnixSite(runner, path='/run/docker/plugins/vlad.sock') await site.start() logging.info("Trying to open the plugin socket...") try: while True: await asyncio.sleep(3600) # sleep forever by 1 hour intervals except KeyboardInterrupt: print("Got CTRL+C, shutting down...") except Exception as e: print(e) finally: await runner.cleanup() # If we created the unix socket, clean it up if isinstance(site, web.UnixSite): os.unlink('/run/docker/plugins/vlad.sock')
async def link_httpserver(self): ws_port = await self.web_port_chan.recv() # init http server handlers url_response = { "ins_url": self._api._ins_url, "md_url": self._api._md_url, "ws_url": 'ws://%s:%s' % (self._http_server_host, str(ws_port['port'])) } # TODO:在复盘模式下发送 replay_dt 给 web 端,服务器改完后可以去掉 if isinstance(self._api._backtest, tqsdk.api.TqReplay): url_response["replay_dt"] = int( datetime.combine(self._api._backtest._replay_dt, datetime.min.time()).timestamp() * 1e9) app = web.Application() app.router.add_get(path='/url', handler=lambda request: TqWebHelper. httpserver_url_handler(url_response)) app.router.add_get(path='/', handler=lambda request: TqWebHelper. httpserver_index_handler(self._web_dir)) app.router.add_static('/', self._web_dir, show_index=True) runner = web.AppRunner(app) await runner.setup() server_socket = socket.socket() server_socket.bind((self._http_server_host, self._http_server_port)) address = server_socket.getsockname() site = web.SockSite(runner, server_socket) await site.start() self._logger.info("您可以访问 http://{ip}:{port} 查看策略绘制出的 K 线图形。".format( ip=address[0], port=address[1])) await asyncio.sleep(100000000000)
def after_cli(*args, **kwargs): log = get_logger() loop = asyncio.get_event_loop() if not globvars.executors: msg = 'No model loaded, exiting ...' print(msg, file=sys.stderr) log.warning(msg) sys.exit(1) log.debug('setup webservice runner') runner = web.AppRunner(webservice.app) loop.run_until_complete(runner.setup()) if kwargs['path']: log.info('create webservice site at unix:///%s', kwargs['path']) site = web.SockSite(runner, kwargs['path']) else: log.info('create webservice site at http://%s:%d', kwargs['host'], kwargs['port']) site = web.TCPSite(runner, kwargs['host'], kwargs['port']) log.debug('start webservice site') loop.run_until_complete(site.start()) log.debug('>>> run()') try: loop.run_forever() except Exception: log.exception('Un-caught exception:') raise finally: log.debug('<<< run()') log.info('======== Shutdown ========')
async def link_httpserver(self): try: url_response = { "ins_url": self._api._ins_url, "md_url": self._api._md_url, } # TODO:在复盘模式下发送 replay_dt 给 web 端,服务器改完后可以去掉 if isinstance(self._api._backtest, tqsdk.api.TqReplay): url_response["replay_dt"] = int(datetime.combine(self._api._backtest._replay_dt, datetime.min.time()).timestamp() * 1e9) app = web.Application() app.router.add_get(path='/url', handler=lambda request: TqWebHelper.httpserver_url_handler(url_response)) app.router.add_get(path='/', handler=self.httpserver_index_handler) app.router.add_get(path='/index.html', handler=self.httpserver_index_handler) app.router.add_static('/web', self._web_dir, show_index=True) app.add_routes([web.get('/ws', self.connection_handler)]) runner = web.AppRunner(app) await runner.setup() server_socket = socket.socket() server_socket.bind((self._http_server_host, self._http_server_port)) address = server_socket.getsockname() site = web.SockSite(runner, server_socket) await site.start() ip = "127.0.0.1" if address[0] == "0.0.0.0" else address[0] self._logger.info("您可以访问 http://{ip}:{port} 查看策略绘制出的 K 线图形。".format(ip=ip, port=address[1])) await asyncio.sleep(100000000000) finally: await runner.shutdown() await runner.cleanup()
async def _run(self) -> None: if isinstance(self.wsgi, Application): app = self.wsgi elif asyncio.iscoroutinefunction(self.wsgi): app = await self.wsgi() else: raise RuntimeError( "wsgi app should be either Application or " "async function returning Application, got {}".format( self.wsgi)) access_log = self.log.access_log if self.cfg.accesslog else None runner = web.AppRunner(app, logger=self.log, keepalive_timeout=self.cfg.keepalive, access_log=access_log, access_log_format=self._get_valid_log_format( self.cfg.access_log_format)) await runner.setup() aiomonitor.start_monitor(loop=self.loop, locals={"app": app}, monitor=Tap2GoMonitor) ctx = self._create_ssl_context(self.cfg) if self.cfg.is_ssl else None runner = runner server = runner.server for sock in self.sockets: site = web.SockSite(runner, sock, ssl_context=ctx, shutdown_timeout=self.cfg.graceful_timeout / 100 * 95) await site.start() # If our parent changed then we shut down. pid = os.getpid() try: while self.alive: # type: ignore self.notify() cnt = server.requests_count if self.cfg.max_requests and cnt > self.cfg.max_requests: self.alive = False self.log.info("Max requests, shutting down: %s", self) elif pid == os.getpid() and self.ppid != os.getppid(): self.alive = False self.log.info("Parent changed, shutting down: %s", self) else: await self._wait_next_notify() except BaseException: pass await runner.cleanup()
async def test_site_double_added(make_runner) -> None: _sock = get_unused_port_socket("127.0.0.1") runner = make_runner() await runner.setup() site = web.SockSite(runner, _sock) await site.start() with pytest.raises(RuntimeError): await site.start() assert len(runner.sites) == 1
async def _async_start(self): """Start the site.""" handler = EventNotifyHandler() app = web.Application() app.add_routes([web.route("notify", "", handler.notify)]) self.runner = web.AppRunner(app) await self.runner.setup() self.site = web.SockSite(self.runner, self.sock) await self.site.start() log.debug("Event listener running on %s", (self.ip_address, self.port))
async def test_addresses(make_runner, unix_sockname) -> None: _sock = get_unused_port_socket("127.0.0.1") runner = make_runner() await runner.setup() tcp = web.SockSite(runner, _sock) await tcp.start() unix = web.UnixSite(runner, unix_sockname) await unix.start() actual_addrs = runner.addresses expected_host, expected_post = _sock.getsockname()[:2] assert actual_addrs == [(expected_host, expected_post), unix_sockname]
async def start(self, ssl_context: Optional[ssl.SSLContext] = None) -> None: app = web.Application() app.add_routes(routes) runner = web.AppRunner(app) await runner.setup() self.site = web.SockSite(runner=runner, sock=self.sock, ssl_context=ssl_context) await self.site.start() logger.info("Server is up", port=self.port)
def main(): server_loop = asyncio.new_event_loop() server_loop.create_server print('listening localhost:%d' % port) asyncio.set_event_loop(server_loop) runner = start_server() server_loop.run_until_complete(runner.setup()) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('localhost', port)) site = web.SockSite(runner, sock) server_loop.run_until_complete(site.start()) server_loop.run_forever()
async def test_addresses(make_runner, shorttmpdir) -> None: _sock = get_unused_port_socket('127.0.0.1') runner = make_runner() await runner.setup() tcp = web.SockSite(runner, _sock) await tcp.start() path = str(shorttmpdir / 'tmp.sock') unix = web.UnixSite(runner, path) await unix.start() actual_addrs = runner.addresses expected_host, expected_post = _sock.getsockname()[:2] assert actual_addrs == [(expected_host, expected_post), path]
async def start_server(): app = web.Application() app['q'] = asyncio.Queue() app.add_routes(routes) runner = web.AppRunner(app) await runner.setup() sock = socket.socket() sock.bind(("127.0.0.1", 0)) sock.listen(128) _, port = sock.getsockname() site = web.SockSite(runner, sock, shutdown_timeout=0) await site.start() return (runner, port)
async def start_runner(runner, conf: config.Config): await runner.setup() site: Optional[Union[web.TCPSite, web.UnixSite, web.SockSite]] = None if conf.port is not None: print(f"Starting HTTP server on localhost:{conf.port}") site = web.TCPSite(runner, 'localhost', conf.port) elif conf.socket is not None: print(f"Starting UNIX socket server on {conf.socket}") site = web.UnixSite(runner, conf.socket) elif conf.socket_fd is not None: print(f"Starting UNIX socket server on FD {conf.socket_fd}") sock = socket.socket(fileno=conf.socket_fd) site = web.SockSite(runner, sock) assert site is not None, "Invalid config, no listening address" return await site.start()
async def _run_app(self) -> web.AppRunner: runner = web.AppRunner(self.app, logger=self.log, keepalive_timeout=15.0, access_log=None) await runner.setup() assert runner is not None server = runner.server assert server is not None for sock in self.sock: site = web.SockSite(runner, sock=sock, shutdown_timeout=self.graceful_timeout / 100 * 95) await site.start() return runner
async def run(self): """ Setting up Gate, routes and main socket """ app = web.Application() app["api_gate"] = Gate() app.add_routes([ web.get('/api/v1/{entity}', entry_points.api), ]) runner = web.AppRunner(app) await runner.setup() sock = self.__make_socket() srv = web.SockSite(runner, sock) await srv.start() return srv, app, runner
async def _start_interface(self, args) -> Optional[web.AppRunner]: routes = self.routes() # socket config is 'unset' when run as a standalone process if args.socket == 'unset': if len(routes) == 0: return None # no routes requested, nothing to do # create a local server app = web.Application() app.add_routes(routes) runner = web.AppRunner(app) await runner.setup() site = web.TCPSite(runner, port=args.port, host=args.host) await site.start() print("starting web server at %s:%d" % (args.host, args.port)) return runner # socket config is 'none' when joule does not connect a socket if args.socket == 'none': if len(routes) > 0: logging.error( "No socket available for the interface, check module configuration" ) return None # otherwise start a UNIX runner on the socket if os.path.exists(args.socket): log.error( "Socket address [%s] is already in use, cannot start interface" % socket) return None app = web.Application() app.add_routes(routes) runner = web.AppRunner(app) await runner.setup() sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.bind(args.socket) site = web.SockSite(runner, sock) await site.start() print("starting web server at [%s]" % args.socket) return runner
async def start_server(): try: host = "127.0.0.1" port = 8000 reuseport = True app = web.Application() app.add_routes([ web.get('/api/simple', simple), web.get('/api/big', bigdata), web.get('/api/cpu', cpubounded), web.get('/api/ping', ping), ]) runner = web.AppRunner(app) await runner.setup() sock = mk_socket(host, port, reuseport=reuseport) srv = web.SockSite(runner, sock) await srv.start() print('Server started') return srv, app, runner except Exception: traceback.print_exc() raise
async def start_servers(self): test_page_path = pathlib.Path(__file__).with_name("test_page.html") async def handle_test_page(request: web.Request) -> web.StreamResponse: with test_page_path.open("r", encoding="utf-8") as f: return web.Response(text=f.read(), headers={hdrs.CONTENT_TYPE: "text/html"}) async def handle_no_cors(request: web.Request) -> web.StreamResponse: return web.Response( text="""{"type": "no_cors.json"}""", headers={hdrs.CONTENT_TYPE: "application/json"}) async def handle_resource(request: web.Request) -> web.StreamResponse: return web.Response( text="""{"type": "resource"}""", headers={hdrs.CONTENT_TYPE: "application/json"}) async def handle_servers_addresses( request: web.Request) -> web.StreamResponse: servers_addresses = \ {name: descr.url for name, descr in self.servers.items()} return web.Response(text=json.dumps(servers_addresses)) class ResourceView(web.View, CorsViewMixin): async def get(self) -> web.StreamResponse: return await handle_resource(self.request) # For most resources: # "origin" server has no CORS configuration. # "allowing" server explicitly allows CORS requests to "origin" server. # "denying" server explicitly disallows CORS requests to "origin" # server. # "free_for_all" server allows CORS requests for all origins server. # "no_cors" server has no CORS configuration. cors_server_names = ["allowing", "denying", "free_for_all"] server_names = cors_server_names + ["origin", "no_cors"] for server_name in server_names: assert server_name not in self.servers self.servers[server_name] = _ServerDescr() server_sockets = {} # Create applications and sockets. for server_name, server_descr in self.servers.items(): server_descr.app = web.Application() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(("127.0.0.1", 0)) sock.listen(10) server_sockets[server_name] = sock hostaddr, port = sock.getsockname() server_descr.url = "http://{host}:{port}".format(host=hostaddr, port=port) # Server test page from origin server. self.servers["origin"].app.router.add_route("GET", "/", handle_test_page) self.servers["origin"].app.router.add_route("GET", "/servers_addresses", handle_servers_addresses) # Add routes to all servers. for server_name in server_names: app = self.servers[server_name].app app.router.add_route("GET", "/no_cors.json", handle_no_cors) if self.use_webview: app.router.add_route("*", "/cors_resource", ResourceView, name="cors_resource") else: app.router.add_route("GET", "/cors_resource", handle_resource, name="cors_resource") cors_default_configs = { "allowing": { self.servers["origin"].url: ResourceOptions(allow_credentials=True, expose_headers="*", allow_headers="*") }, "denying": { # Allow requests to other than "origin" server. self.servers["allowing"].url: ResourceOptions(allow_credentials=True, expose_headers="*", allow_headers="*") }, "free_for_all": { "*": ResourceOptions(allow_credentials=True, expose_headers="*", allow_headers="*") }, } # Configure CORS. for server_name, server_descr in self.servers.items(): default_config = cors_default_configs.get(server_name) if default_config is None: continue server_descr.cors = _setup(server_descr.app, defaults=default_config) # Add CORS routes. for server_name in cors_server_names: server_descr = self.servers[server_name] # TODO: Starting from aiohttp 0.21.0 name-based access returns # Resource, not Route. Manually get route while aiohttp_cors # doesn't support configuring for Resources. resource = server_descr.app.router["cors_resource"] route = next(iter(resource)) if self.use_resources: server_descr.cors.add(resource) server_descr.cors.add(route) elif self.use_webview: server_descr.cors.add(route) else: server_descr.cors.add(route) # Start servers. for server_name, server_descr in self.servers.items(): runner = web.AppRunner(server_descr.app) await runner.setup() site = web.SockSite(runner, server_sockets[server_name]) await site.start() server_descr.runner = runner self._logger.info("Started server '%s' at '%s'", server_name, server_descr.url)
async def _run_app( self, *, host: Optional[Union[str, web.HostSequence]] = None, port: Optional[int] = None, path: Optional[str] = None, sock: Optional[socket.socket] = None, shutdown_timeout: float = 60.0, ssl_context: Optional[SSLContext] = None, backlog: int = 128, access_log_class: Type[web.AbstractAccessLogger] = web.AccessLogger, access_log_format: str = web.AccessLogger.LOG_FORMAT, access_log: Optional[logging.Logger] = web.access_logger, handle_signals: bool = True, reuse_address: Optional[bool] = None, reuse_port: Optional[bool] = None, ) -> None: # This function is pulled from aiohttp.web._run_app app = self runner = web.AppRunner( app, handle_signals=handle_signals, access_log_class=access_log_class, access_log_format=access_log_format, access_log=access_log, ) await runner.setup() sites = [] try: if host is not None: if isinstance(host, (str, bytes, bytearray, memoryview)): sites.append( web.TCPSite( runner, host, port, shutdown_timeout=shutdown_timeout, ssl_context=ssl_context, backlog=backlog, reuse_address=reuse_address, reuse_port=reuse_port, )) else: for h in host: sites.append( web.TCPSite( runner, h, port, shutdown_timeout=shutdown_timeout, ssl_context=ssl_context, backlog=backlog, reuse_address=reuse_address, reuse_port=reuse_port, )) elif path is None and sock is None or port is not None: sites.append( web.TCPSite( runner, port=port, shutdown_timeout=shutdown_timeout, ssl_context=ssl_context, backlog=backlog, reuse_address=reuse_address, reuse_port=reuse_port, )) if path is not None: if isinstance(path, (str, bytes, bytearray, memoryview)): sites.append( web.UnixSite( runner, path, shutdown_timeout=shutdown_timeout, ssl_context=ssl_context, backlog=backlog, )) else: for p in path: sites.append( web.UnixSite( runner, p, shutdown_timeout=shutdown_timeout, ssl_context=ssl_context, backlog=backlog, )) if sock is not None: if not isinstance(sock, Iterable): sites.append( web.SockSite( runner, sock, shutdown_timeout=shutdown_timeout, ssl_context=ssl_context, backlog=backlog, )) else: for s in sock: sites.append( web.SockSite( runner, s, shutdown_timeout=shutdown_timeout, ssl_context=ssl_context, backlog=backlog, )) for site in sites: await site.start() names = sorted(str(s.name) for s in runner.sites) logger.debug("Running EventSub server on {}".format( ", ".join(names))) await self._closing.wait() finally: await runner.cleanup()