python - 如何使用 brotli 压缩 python 发出 http 请求

标签 python python-2.7 brotli

我有一个启用了 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/

相关文章:

python - Pandas 在 Python 中将一些行转换为列

python - 如何在 Pyramid 休息框架中添加您的应用程序特定设置?

google-app-engine - 如何在 Google App Engine 上获取 gzip 或 brotli 编码

reactjs - 将 brotli 与 AWS Cloudfront + S3 结合使用

python - python audiotools卡在get_metadata()

python - TypeError : list indices must be integers, 不是字典

python - Scrapy 重试中间件失败并显示非标准 http 状态代码

python - 通过转换为PNG使JPEG图像中的背景透明

python - 返回前打印值

compression - 如何使用 brotli 命令行(版本 0.5.2)压缩文件夹?