流式响应

流式响应允许分块读取响应体,无需将整个响应加载到内存。适用于大文件下载、SSE 等场景。

章节概览

章节说明示例文件
异步流式下载Net 流式请求streaming.py
同步流式下载SyncNet 流式请求streaming.py
StreamResponse 属性流响应对象
多引擎支持各引擎流式兼容性

异步流式下载

from hs_net import Net

async with Net() as net:
    resp = await net.stream("GET", "https://example.com/large-file.zip")
    async with resp:
        with open("file.zip", "wb") as f:
            async for chunk in resp:
                f.write(chunk)

同步流式下载

from hs_net import SyncNet

with SyncNet() as net:
    with net.stream("GET", "https://example.com/large-file.zip") as resp:
        with open("file.zip", "wb") as f:
            for chunk in resp:
                f.write(chunk)

StreamResponse 属性

流式响应对象提供基本的响应信息:

属性类型说明
urlstr响应 URL
status_codeint状态码
headersdict响应头
cookiesdict响应 cookies
okbool状态码是否为 2xx
注意

流式响应不支持 textjson_datacss()xpath() 等属性和方法。 如需解析响应内容,请使用普通的 request() / get() 方法。

流式响应使用后必须关闭,推荐使用 with / async with 上下文管理器。

多引擎支持

所有引擎都支持流式响应:

with SyncNet(engine="curl_cffi") as net:
    with net.stream("GET", url) as resp:
        for chunk in resp:
            process(chunk)