【问题标题】:Python Aiohttp client - disable encoding redirected urlPython Aiohttp 客户端 - 禁用编码重定向 url
【发布时间】:2021-08-08 07:02:03
【问题描述】:

我正在使用 aiohttp 客户端向 URL 发送请求并收集重定向的 URL。

在我的例子中,重定向的 URL 中包含 Unicode 文本。我希望它保持原样。

例如,实际重定向的 URL 是 example.com/förderprojekte-e-v,但 aiohttp 客户端会自动对其进行编码并返回给我 example.com/f\udcf6rderprojekte-e-v .

如何使 aiohttp 禁用重定向 url 的自动编码。

对于请求模块,this 解决方案有效,但我需要 aiohttp 的帮助。

我的代码:

async fetch(url):
    #url = 'https://www.example.com/test/123'
    async with client.get(url, allow_redirects = True ) as resp:
        html = await resp.read()
        redir_url = str(resp.url)
        #example.com/f\udcf6rderprojekte-e-v

或者至少告诉我如何将 \udcf6 转换为 ö

【问题讨论】:

    标签: python python-requests urlencode aiohttp


    【解决方案1】:
    async def handle_redirected_url(request):
        # https://
        scheme = request.scheme
        # example.com
        host = request.host
        # /f\udcf6rderprojekte-e-v
        unencoded_path = request.raw_path
        
        unencoded_url = scheme+host+unencoded_path
        return web.json_response(status=200, data={'unencoded_url': unencoded_url)
    

    查看https://docs.aiohttp.org/en/stable/web_reference.html中的请求对象属性

    【讨论】:

    • 我更新了问题中的代码。你能解释一下你的答案是如何工作的吗?我的问题是关于 aiohttp 客户端的,你的回答好像是 aiohttp 服务器。
    猜你喜欢
    • 2015-07-15
    • 2021-06-24
    • 2020-04-15
    • 2019-01-16
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多