Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asyncio and aiohttp route all urls paths to handler

I have trouble to find a wildcard url matching pattern which matches all incoming urls. This just matches a url which has nothing more than the hostname:

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 

So it should call the handler anytime there is a request regardless of the path. If its http://127.0.0.1:9999/ or http://127.0.0.1:9999/test/this/test/

I looked it up here http://aiohttp.readthedocs.org/en/stable/web.html#aiohttp-web-variable-handler with no success for the right clue

like image 899
Jurudocs Avatar asked Jan 02 '16 12:01

Jurudocs


1 Answers

You can use app.router.add_route('GET', '/{tail:.*}', handle) for catching all urls.

The part after the colon (:) is a regular expression. .* regexp describes everything, including path separators (/) and other symbols.

like image 52
Andrew Svetlov Avatar answered Oct 17 '22 22:10

Andrew Svetlov