2016-01-02 15 views
10

Ho difficoltà a trovare un modello di corrispondenza URL con caratteri jolly che corrisponda a tutti gli URL in arrivo. Questo corrisponde a solo un URL che non ha nulla più che l'hostname:Asyncio e aiohttp instradano tutti i percorsi degli URL verso l'handler

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 

Così dovrebbe chiamare il gestore ogni volta che c'è una richiesta indipendentemente dal percorso. Se il suo http://127.0.0.1:9999/ o http://127.0.0.1:9999/test/this/test/

Ho cercato qui http://aiohttp.readthedocs.org/en/stable/web.html#aiohttp-web-variable-handler senza successo per l'indizio giusto

risposta

18

È possibile utilizzare app.router.add_route('GET', '/{tail:.*}', handle) per la cattura di tutti gli URL.

Problemi correlati