我有一个启用了 brotli 压缩的 Apache 服务器。 当我从 cli 发送curl 请求时,它工作正常。然而,当我从 python 发送请求时,它返回没有 brotli 压缩的纯文本。这意味着它没有使用 brotli 压缩。
有效的 Curl 命令:
curl -H "Accept-Encoding: br" http://testsite/index.html
这是Python代码:
headers = {
' Accept-Encoding': 'br',
}
response = requests.get('http://testsite/index.html', headers=headers)
如何从 python 发出请求,以便它向服务器发出请求以使用 brotli 压缩。
最佳答案
这支持 brotli 请求 https://github.com/encode/httpx .
单元测试示例:
import brotli
import httpx
body = b"test 123"
compressed_body = brotli.compress(body)
headers = [(b"Content-Encoding", b"br")]
response = httpx.Response(200, headers=headers, content=compressed_body)
关于python - 如何使用 brotli 压缩 python 发出 http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57261619/