gpt4 book ai didi

python-3.x - 捕获 Aiohttp 中的所有路由

转载 作者:行者123 更新时间:2023-12-05 04:36:39 26 4
gpt4 key购买 nike

我怎样才能从接收到的请求中完全捕获所有路径?我正在使用以下代码,但它不起作用。

routes = web.RouteTableDef()

@routes.get(path='/{key}')
async def _(request):
print(request)
await self.processing_request(request=request, post=False)

@routes.post(path='/{key}')
async def _(request):
await self.processing_request(request=request, post=True)

最佳答案

变量路径的默认正则表达式是 [^{}/]+,即它不会匹配正斜杠。您可以使用 {variablename:regex} 语法在路径字符串中指定自定义正则表达式:

@routes.get(path="/{key:.+}")
async def _(request):
...

来自 the documentation :

A variable part is specified in the form {identifier}, where theidentifier can be used later in a request handler to access thematched value for that part. This is done by looking up the identifierin the Request.match_info mapping ...

By default, each part matches the regular expression [^{}/]+.

You can also specify a custom regex in the form {identifier:regex}:

(免责声明:我没有测试过)

关于python-3.x - 捕获 Aiohttp 中的所有路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70783518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com