async def init(loop): app = web.Application(loop=loop) app = web_runner.AppRunner(app=app).app() app.router.add_post('/cmd/', cmd, name='cmd', expect_handler=web.Request.json) srv = await loop.create_server(app.make_handler(), '127.0.0.1', 8000) print('Server started at http://127.0.0.1:8000...') return srv
def init(): app = web.Application() app = web_runner.AppRunner(app=app).app() app.router.add_route('GET', '/', index) srv = yield from loop.create_server(app._make_handler(), '127.0.0.1', 9000) logging.info('server started at http://127.0.0.1:9000...') return srv
def init(loop): # 创建数据库池:数据库的账户和密码,以及db需要首先创建好!!! yield from orm.create_pool(loop=loop, **configs.db) # middleware是一种拦截器,URL在被某个函数处理前,可以经过一系列的 middleware 的处理 app = web.Application( loop=loop, middlewares=[logger_factory, auth_factory, response_factory]) # 适配python3.0 app = web_runner.AppRunner(app=app).app # 手动添加路由: # app.router.add_route('GET','/testIndex',index) # app.router.add_route('GET','/testHello',hello) # 初始化模板引擎:绑定了block 和 init_jinja2(app, filters=dict(datetime=datetime_filter)) # 给web app添加路由,统一放在handlers模块中处理 add_routes(app, 'handlers') # 注册静态文件 add_static(app) logging.info('server before...') # 创建1个服务器对象 host = '127.0.0.1' # 或 'localhost' == '127.0.0.1' port = 9003 server = yield from loop.create_server(app.make_handler(), host, port) logging.info('server start at http://%s:%s' % (host, port)) return server
def initialize(self): self.runner = web_runner.AppRunner(self.aioapp) self.loop.run_until_complete(self.runner.setup()) for host in ["::", "0.0.0.0"]: site = web_runner.TCPSite(self.runner, host=host, port=self.port, ssl_context=self.ssl_context) _LOGGER.debug("Starting for port %s ..", self.port) self.loop.run_until_complete(site.start())
async def init(loop, address, port): # init 协程产出一个服务器,交给事件循环驱动。 app = web.Application() # aiohttp.web.Application 类表示 Web 应用 app = web_runner.AppRunner(app=app).app() app.router.add_route('GET', '/', home) handler = app._make_handler() """ app.make_handler 方法返回一个 aiohttp.web.RequestHandler 实例, 根据 app 对象设置的路由处理 HTTP 请求. """ server = await loop.create_server(handler, address, port) """ create_server 方法创建服务器,以 handler 为协议处理程序, 并把服务器绑定在指定 的地址(address)和端口(port)上。 """ return server.sockets[0].getsockname()
async def prepare(self): self.app.log_info("Preparing to start http server") self._runner = web_runner.AppRunner( self.web_app, handle_signals=True, access_log_class=helpers.AccessLogger, access_log_format=helpers.AccessLogger.LOG_FORMAT, access_log=self.access_log) await self._runner.setup() self._sites = [] self._sites.append( web_runner.TCPSite(self._runner, self.host, self.port, shutdown_timeout=self.shutdown_timeout, ssl_context=None, backlog=128))
async def prepare(self): self.app.log_info("Preparing to start http server") self.web_app = web.Application(loop=self.loop, middlewares=self.middlewares) await self.handler.prepare() self._runner = web_runner.AppRunner( self.web_app, handle_signals=False, access_log_class=self.access_log_class, access_log_format=self.access_log_format, access_log=self.access_log) await self._runner.setup() self._sites = [] self._sites.append( web_runner.TCPSite(self._runner, self.host, self.port, shutdown_timeout=self.shutdown_timeout, ssl_context=self.ssl_context, backlog=self.backlog, reuse_address=self.reuse_address, reuse_port=self.reuse_port))