当前位置: 代码网 > it编程>前端脚本>Python > Python实现HTTP上行请求体的gzip压缩

Python实现HTTP上行请求体的gzip压缩

2026年07月29日 Python 我要评论
前言先重申核心原理:上行请求体压缩不是http库自带功能,本质是你自己把body gzip压缩,带上 content-encoding: gzip 请求头,将压缩二进制作为请求体发送。所有支持自定义请

前言

先重申核心原理:上行请求体压缩不是http库自带功能,本质是你自己把body gzip压缩,带上 content-encoding: gzip 请求头,将压缩二进制作为请求体发送。

所有支持自定义请求body、自定义请求头的http客户端都能实现,区别只是各个库传参名称不一样:

  • requests:data=
  • httpx:content=
  • aiohttp:data=
  • urllib:data=
  • pycurl:postfields

下面分「同步库」「异步库」给出完整示例,统一复用gzip压缩函数。

通用压缩工具函数(所有示例共用)

import gzip
from io import bytesio

def gzip_compress(raw_bytes: bytes) -> bytes:
    buf = bytesio()
    with gzip.gzipfile(fileobj=buf, mode="wb") as f:
        f.write(raw_bytes)
    return buf.getvalue()

一、标准库:urllib(无需额外安装,内置)

无需安装第三方包,适合环境受限不能装requests的场景

import json
from urllib import request

url = "http://127.0.0.1:8000/api/upload"
payload = {"data": list(range(2000))}
raw = json.dumps(payload).encode("utf-8")
compressed_body = gzip_compress(raw)

headers = {
    "content-type": "application/json; charset=utf-8",
    "content-encoding": "gzip"
}

req = request.request(url, data=compressed_body, headers=headers, method="post")
with request.urlopen(req) as resp:
    print(resp.read().decode("utf-8"))

缺点:api简陋,不支持连接池、超时易用性差,生产一般不首选。

二、httpx(同步 + 异步,当前主流推荐)

新一代http客户端,兼容requests风格,支持http1.1/http2

import httpx
import json

url = "http://127.0.0.1:8000/api/upload"
payload = {"data": list(range(2000))}
raw = json.dumps(payload).encode("utf-8")
compressed_body = gzip_compress(raw)

headers = {
    "content-type": "application/json; charset=utf-8",
    "content-encoding": "gzip"
}

# 同步
with httpx.client(timeout=10) as client:
    res = client.post(url, headers=headers, content=compressed_body)
    print(res.text)

# 异步版本
"""
import asyncio
async def run():
    async with httpx.asyncclient(timeout=10) as client:
        res = await client.post(url, headers=headers, content=compressed_body)
        print(res.text)
asyncio.run(run())
"""

httpx 传二进制body参数名是 content,不是data!

三、aiohttp(异步首选,高并发场景)

异步生态标准http客户端,爬虫、异步微服务大量使用

import aiohttp
import asyncio
import json

async def main():
    url = "http://127.0.0.1:8000/api/upload"
    payload = {"data": list(range(2000))}
    raw = json.dumps(payload).encode("utf-8")
    compressed_body = gzip_compress(raw)

    headers = {
        "content-type": "application/json; charset=utf-8",
        "content-encoding": "gzip"
    }
    async with aiohttp.clientsession() as session:
        async with session.post(url, data=compressed_body, headers=headers) as resp:
            print(await resp.text())

asyncio.run(main())

四、pycurl(底层封装libcurl,高性能)

基于c库libcurl,吞吐性能极强,适合超高并发场景;缺点:windows安装麻烦、api晦涩

import pycurl
import json
from io import bytesio

url = "http://127.0.0.1:8000/api/upload"
payload = {"data": list(range(2000))}
raw = json.dumps(payload).encode("utf-8")
compressed_body = gzip_compress(raw)

resp_buffer = bytesio()
c = pycurl.curl()
c.setopt(c.url, url)
c.setopt(c.post, 1)
c.setopt(c.postfields, compressed_body)
c.setopt(c.writedata, resp_buffer)

headers = [
    "content-type: application/json; charset=utf-8",
    "content-encoding: gzip"
]
c.setopt(c.httpheader, headers)
c.perform()
c.close()

print(resp_buffer.getvalue().decode())

五、tornado.httpclient(tornado框架内置异步客户端)

如果你项目本身使用tornado web框架,可以直接使用内置客户端,无需额外依赖

import json
from tornado.httpclient import asynchttpclient, httprequest
import tornado.ioloop

async def fetch():
    url = "http://127.0.0.1:8000/api/upload"
    payload = {"data": list(range(2000))}
    raw = json.dumps(payload).encode("utf-8")
    compressed_body = gzip_compress(raw)

    req = httprequest(
        url,
        method="post",
        body=compressed_body,
        headers={
            "content-type": "application/json; charset=utf-8",
            "content-encoding": "gzip"
        }
    )
    client = asynchttpclient()
    res = await client.fetch(req)
    print(res.body.decode())

tornado.ioloop.ioloop.current().run_sync(fetch)

关键共性注意事项(所有库通用)

不要使用框架自带json参数:例如 httpx.post(json={})requests.post(json={}),这类接口会自动编码,无法替换成压缩二进制,必须手动序列化+使用原始body参数。

下行压缩 ≠ 上行压缩:

  • accept-encoding 只是告诉服务端:我能接收压缩响应;
  • content-encoding: gzip 才代表:本次请求body已经压缩。

服务端必须手动解压:绝大多数web框架默认不会自动解压带有content-encoding的请求体,缺少解压代码会直接收到乱码二进制。

库选型建议

  1. 新项目、同步+异步都需要 → httpx
  2. 纯异步高并发服务/爬虫 → aiohttp
  3. 不能安装第三方包、极简环境 → urllib(标准库)
  4. 极致性能、熟悉libcurl → pycurl
  5. 老旧项目维持现状 → requests

到此这篇关于python实现http上行请求体的gzip压缩的文章就介绍到这了,更多相关python http请求体压缩内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com