gpt4 book ai didi

python - 如何使用 aiohttp_jinja2 获取 url 的一部分?

转载 作者:行者123 更新时间:2023-12-04 09:06:55 85 4
gpt4 key购买 nike

所以我试图获取像 http://example/home?code=123456789 这样的 URL 的一部分,这个 123456789 每次都会改变,因为它是 oauth
所以我想得到它
这是py文件


from aiohttp import web, web_urldispatcher

import discord
from discord.ext import commands


import aiohttp_jinja2

import jinja2
from pathlib import Path


from oauth import Ouath


@aiohttp_jinja2.template('home.html')
async def start(request):
raise web.HTTPSeeOther(location=Ouath.discord_login_url)



@aiohttp_jinja2.template('home.html')
async def login(request):
return
app = web.Application(loop=self.client.loop)
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader(str(here)))
app.router.add_get('/', start)
app.router.add_get('/home', login)
runner = web.AppRunner(app)
await runner.setup()
self.site = web.TCPSite(runner, '127.0.0.1', 5000)
await self.client.wait_until_ready()
await self.site.start()
我想打印在 html 文件中
但我不知道如何得到那部分
注意:我编辑了代码框

最佳答案

在挖掘 aiohttp_jinja2 的源代码后和 aiohttp看来你可以通过 request.query.get('code') 得到它

@aiohttp_jinja2.template('home.html') 
async def login(request):
#print('code:', request.query.get('code'))
return {'code': request.query.get('code')}
如果没有 ?code=...在 URL 然后它给出 None但您可以使用 request.query.get('code', some_default_value) 设置其他默认值

文档 aiohttp : web.BaseRequest.query

关于python - 如何使用 aiohttp_jinja2 获取 url 的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63421155/

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