Asyncio和aiohttp将所有urls路径路由到处理程序

时间:2016-01-02 12:06:50

标签: python python-3.x python-asyncio aiohttp

我很难找到匹配所有传入网址的通配符网址匹配模式。这只匹配一个只有主机名的URL:

import asyncio
from aiohttp import web

@asyncio.coroutine
def handle(request):
    print('there was a request')
    text = "Hello "
    return web.Response(body=text.encode('utf-8'))

@asyncio.coroutine
def init(loop):
    app = web.Application(loop=loop)
    app.router.add_route('GET', '/', handle)

    srv = yield from loop.create_server(app.make_handler(),
                                        '127.0.0.1', 9999)
    print("Server started at http://'127.0.0.1:9999'")
    return srv

loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
try:
    loop.run_forever()
except KeyboardInterrupt:
    pass 

因此无论路径如何,它都应该在有请求时随时调用处理程序。如果是http://127.0.0.1:9999/http://127.0.0.1:9999/test/this/test/

我在这里http://aiohttp.readthedocs.org/en/stable/web.html#aiohttp-web-variable-handler查找了正确的线索

没有成功

1 个答案:

答案 0 :(得分:28)

您可以使用WebView来捕获所有网址。