首页 > 解决方案 > 如何在 aiohttp 中设置查询处理程序

问题描述

我有下一个代码:

from aiohttp import web

PORT = 8080
HOST = 'localhost'

routes = web.RouteTableDef()


@routes.get('/do_something?from={f}&to={s}')
async def test_get(request):
    return web.json_response({request.match_info['f']: request.match_info['s']})
    #return web.json_response({request.match_info['FROM']: request.match_info['TO']})


def start_app():
    app = web.Application()
    app.add_routes(routes)
    web.run_app(app, port=PORT, host=HOST)


if __name__ == '__main__':
    start_app()

我的问题在于我无法处理如下链接: http://localhost:8080/get_something?from=20&to=24

标签: pythonhttpaiohttp

解决方案


订阅/do_something( @routes.get('/do_something'))。所有查询参数都传递到匹配的 Web 处理程序中。


推荐阅读