【问题标题】:How to make http request using brotli compression python如何使用brotli压缩python发出http请求
【发布时间】:2019-07-29 21:25:47
【问题描述】:

我有一个启用了 brotli 压缩的 Apache 服务器。 当我从我的 cli 发送 curl 请求时,它工作正常。但是,当我从 python 发送请求时,它会返回没有 brotli 压缩的纯文本。这意味着它没有使用 brotli 压缩。

有效的卷曲命令:

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 压缩。

【问题讨论】:

  • 是不是因为Accept-Encoding前面有空格?
  • 不,空间没有任何区别。

标签: python python-2.7 brotli


【解决方案1】:

这支持 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)

【讨论】:

    【解决方案2】:

    您的示例未显示您如何阅读回复。但是你使用requests依赖urllib3,请求响应内容首先根据content-encoding header进行解码。

    你可以在urllib3 documentation看到参数。

    作为brotli decoding is supported by urllib3,那么您收到解码后的内容实际上是有意义的(考虑到安装了brotli模块)。

    您是否实际请求访问 brotli 编码的内容,如果是这样,您必须检查请求文档以查看是否有访问原始未解码内容的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-21
      • 1970-01-01
      • 2013-12-28
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      • 2017-01-03
      相关资源
      最近更新 更多