gpt4 book ai didi

python - 使用python3.5的aiohttp查询get URL的参数

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:58 27 4
gpt4 key购买 nike

async def method(request):
## here how to get query parameters
param1 = request.rel_url.query['name']
param2 = request.rel_url.query['age']
return web.Response(text=str(result))


if __name__ == '__main__':
app = web.Application()
app.router.add_route('GET', "/sample", method)

web.run_app(app,host='localhost', port=3000)

以上代码运行于python 3.6。我需要从示例 URL http://localhost.com/sample?name=xyz&age=xy 中提取查询参数值 (xyz & xy)。我尝试了 req.rel_url.queryrequest.query_string。它只给出了第一个参数值 xyz,但我没有得到查询中的第二个参数 xy(年龄)。

如何获取两个查询值?

最佳答案

这里你几乎没有错误。

  1. result 未在您的函数中定义。您以正确的方式获取参数,但随后由于未定义 result
  2. 而发生错误
  3. 您的目标是 localhost.com,不确定它在您的机器上是如何设置的,但它不应该工作。

所以,这是工作示例:

from aiohttp import web

async def method(request):
## here how to get query parameters
param1 = request.rel_url.query['name']
param2 = request.rel_url.query['age']
result = "name: {}, age: {}".format(param1, param2)
return web.Response(text=str(result))


if __name__ == '__main__':
app = web.Application()
app.router.add_route('GET', "/sample", method)

web.run_app(app,host='localhost', port=11111)

那你可以试试:http://localhost:11111/sample?name=xyz&age=xy它正在运行。

关于python - 使用python3.5的aiohttp查询get URL的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47851096/

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