本文整理汇总了Python中aiohttp.web.Application.reverse_url方法的典型用法代码示例。如果您正苦于以下问题:Python Application.reverse_url方法的具体用法?Python Application.reverse_url怎么用?Python Application.reverse_url使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiohttp.web.Application
的用法示例。
在下文中一共展示了Application.reverse_url方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: HTTPModule
# 需要导入模块: from aiohttp.web import Application [as 别名]
# 或者: from aiohttp.web.Application import reverse_url [as 别名]
class HTTPModule(Module):
export_as = 'http'
def __init__(self, *args, **kwds):
super().__init__(*args, **kwds)
self._app = Application()
self._server = None
self._handler = None
self._started = False
def add_route(self, method, path, handler):
self._app.router.add_route(method, path, handler)
# Start when the first handlers are added
self._maybe_start()
def add_routes(self, routes):
# routes is a list of (method, path, handler) tuples
for method, path, handler in routes:
self._app.router.add_route(method, path, handler)
# Start when the first handlers are added
self._maybe_start()
def reverse_url(self, name, *args):
baseurl = self.config.get('baseurl', '')
return baseurl + self._app.reverse_url(name, *args)
def _maybe_start(self):
if not self._started:
# Start when the first handlers are added
self._started = True
self.loop.create_task(self._start())
async def _start(self):
address = self.config.get('address', '127.0.0.1')
port = self.config.get('port', 8000)
self.log.info('Starting HTTP server at %s:%s' % (address, port))
self._handler = self._app.make_handler()
self._server = await self.loop.create_server(self._handler, address, port)
def shutdown(self):
if self._started:
self.loop.create_task(self._shutdown_server())
async def _shutdown_server(self):
self._server.close()
await self._server.wait_closed()
await self._app.shutdown()
await self._handler.finish_connections(1.0)
await self._app.cleanup()