问题描述
我使用 aiohttp 发送这样的请求:
I use aiohttp to send a request like so:
async with ClientSession() as session:
res = await session.get("http://0.0.0.0:8000/./")
当我像这样使用 python 启动一个 http 服务器时:python3 -m http.server
When I start up a http server using python like so:python3 -m http.server
我看到路径是规范化的,即服务器得到如下请求:GET/HTTP/1.1" 200
I see that the path is normalized, i.e., the server gets the following request:GET / HTTP/1.1" 200
如何禁用此规范化,以强制执行类似 urrlib 的行为,例如,其中 urllib.request.urlopen("http://0.0.0.0:8000/./")
导致以下请求:GET/./HTTP/1.1
.
How do I disable this normalization, to enforce a behavior like in urrlib, for example, where urllib.request.urlopen("http://0.0.0.0:8000/./")
results in the following request:GET /./ HTTP/1.1
.
推荐答案
aiohttp
使用 yarl 用于 URL 处理.
aiohttp
uses yarl for URL processing.
session.get('http://example.com')
与 session.get(yarl.URL('http://example.com')) 一样有效
您可以为 yarl.URL 禁用 URL 编码使用 encoded=True
,但您必须注意 URL 的正确性.
You can disable URL encoding for yarl.URL with encoded=True
, but you have to take care of URL correctness.
例如
import asyncio
import yarl
import aiohttp
async def test():
url = yarl.URL('https://stackoverflow.com/./', encoded=True)
async with aiohttp.ClientSession() as session:
async with session.get(url, allow_redirects=False) as resp:
print(resp.url)
asyncio.run(test())
这篇关于python:强制aiohttp不规范化url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!