我关注的是一个启用了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压缩的请求。
发布于 2019-09-20 18:35:45
这支持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)发布于 2020-06-12 07:03:34
您的示例没有显示您是如何读取响应的。但是您正在使用依赖于urllib3的requests,请求响应内容首先根据content-encoding header进行解码。
您可以在urllib3 documentation中看到该参数。
作为brotli decoding is supported by urllib3,接收解码后的内容实际上是有意义的(考虑到安装了brotli模块)。
是您想要访问brotli编码的内容的实际请求,如果是这样的话,您将必须检查请求文档,看看是否有方法访问原始的未解码内容。
https://stackoverflow.com/questions/57261619
复制相似问题