Skip to main content

ruyihttp

基于真实 Firefox 161 原生网络组件的 Python 请求库,提供 Requests 风格接口,可模拟 Firefox 100–161 任一版本。 一条 pip install 即可安装,支持 HTTP/1.1、HTTP/2、HTTP/3、会话、代理和流式请求,无需启动浏览器。

Python Platform Stars

保留你熟悉的 get()、post()、Session 和 Response 用法,通过 Python 接口调用随包附带的原生库(Windows DLL / Linux .so)。

pip install -U ruyihttp
import ruyihttp as requests

response = requests.get("https://example.com", timeout=30)
response.raise_for_status()
print(response.text)

快速开始 · 常用功能 · 更多用法 · 赞助商 · 问题反馈

快速开始

支持 Windows x64 和 Linux x86_64,需要 64 位 Python 3.10–3.14。 Linux 需要 glibc 2.31 或更高版本,例如 Ubuntu 20.04+、Debian 11+。 PyPI 上的 0.11.0 目前只提供 Windows x64 wheel,Linux wheel 会在之后的版本补上。 原生库随 wheel 自动安装,Python 依赖由 pip 解析。

python -m pip install -U ruyihttp
python -c "import ruyihttp; print(ruyihttp.__version__)"

也可以从 GitHub Releases 下载 wheel 后用 pip 安装。 Windows 系统如果提示缺少 VCRUNTIME 或 MSVCP,请安装 Microsoft Visual C++ x64 Redistributable。

使用 Session

Session 可复用连接、Cookie 和默认请求头:

import ruyihttp

with ruyihttp.Session() as session:
    session.headers.update({"User-Agent": "my-client/1.0"})
    response = session.get(
        "https://httpbin.org/get",
        params={"page": 1, "keyword": "ruyihttp"},
        timeout=(5, 30),
    )
    response.raise_for_status()
    print(response.json())

常用功能

功能 用法
HTTP 请求 get、post、put、patch、delete、head、options
会话管理 Session、连接复用、Cookie、默认请求头
请求数据 URL 参数、表单、JSON、文件上传、生成器请求体
响应处理 text、content、json()、状态码、响应头
流式下载 stream=True、iter_content()、iter_lines()
代理 HTTP、HTTPS、SOCKS4/5、代理认证
认证与跳转 Basic / Digest 认证、重定向、响应钩子
连接控制 超时、重试、CA 证书、自定义适配器
协议 HTTP/1.1、HTTP/2、通过 HTTPS Alt-Svc 选择 HTTP/3
TLS 信息 response.tls 查看协商协议、版本及会话信息
模拟 Firefox 版本 firefox="100" 到 "161",TLS、请求头、HTTP/2、HTTP/3 全部按该版本发送

更多用法

发送 JSON

import ruyihttp

response = ruyihttp.post(
    "https://httpbin.org/post",
    json={"name": "ruyihttp", "enabled": True},
    timeout=30,
)
response.raise_for_status()
print(response.json())

使用代理

import ruyihttp

response = ruyihttp.get(
    "https://example.com",
    proxies={"https": "http://127.0.0.1:8080"},
    timeout=30,
)

SOCKS 代理先安装可选依赖,再使用 socks5h://127.0.0.1:1080 等代理地址:

pip install "ruyihttp[socks]"

流式下载

import ruyihttp

with ruyihttp.get("https://example.com", stream=True, timeout=30) as response:
    response.raise_for_status()
    with open("page.html", "wb") as output:
        for chunk in response.iter_content(chunk_size=65536):
            output.write(chunk)

配置重试

from ruyihttp import Session
from ruyihttp.adapters import HTTPAdapter
from urllib3.util import Retry

with Session() as session:
    session.mount("https://", HTTPAdapter(max_retries=Retry(total=3)))
    response = session.get("https://example.com", timeout=(5, 30))
    response.raise_for_status()

查看 TLS 信息

import ruyihttp

response = ruyihttp.get("https://example.com", timeout=30)
print(response.tls)

模拟指定 Firefox 版本

默认模拟 Firefox 161。Session 和 get() 等函数都可以用 firefox= 指定 100 到 161 之间的任一正式版,也可以用环境变量 NECKO_FIREFOX 设置默认值:

import ruyihttp

with ruyihttp.Session(firefox="115") as session:
    response = session.get("https://example.com", timeout=30)
    print(response.tls["profile"])   # firefox-115

ruyihttp.get("https://example.com", firefox="128", timeout=30)
print(ruyihttp.FIREFOX_VERSIONS)     # ('100', ..., '161')

选定版本后,网络上发出的内容都按该版本来:

  • TLS 握手(JA3 / JA4 指纹);
  • 请求头:User-Agent(110–119 为 rv:109.0)、Accept、Accept-Language、 Accept-Encoding、Priority;
  • HTTP/2:SETTINGS、PRIORITY 帧和流依赖(Akamai HTTP/2 指纹);
  • HTTP/3:QUIC 握手、传输参数、首包大小、HTTP/3 SETTINGS 和请求头顺序。

同一进程中可以同时使用不同版本的会话。以上各项都和对应版本的官方 Firefox 实测比对过。唯一已知差别:119–154 的 GREASE ECH 扩展比真实浏览器长 1 字节, 不影响 JA3 / JA4。

从源码编译

source/ 包含编译原生运行库所需的全部内容:necko_core.dll 的 C++ 源码、necko_quic.dll 的 Rust 源码(含修改过的 neqo / nss-rs 以及离线依赖 quic/registry),以及 Firefox 161 Windows x64 的 NSS SDK(source/nss-sdk,NSS 为预编译库,不从源码编译)。

需要 Windows x64、Python 3.11+、Rust 1.97+,以及 Firefox mach bootstrap 安装的工具链(~/.mozbuild 下的 clang/、vs/;可用 MOZBUILD_STATE_PATH 指定其他位置)。

python scripts/build_native.py

脚本在 build/native 下编译,并把 necko_core.dll、necko_quic.dll 和 NSS 运行库复制到 ruyihttp/。只编译不覆盖可加 --no-install。

从火狐官方源码生成 NSS SDK

source/nss-sdk 也可以自己从火狐官方源码从头生成。官方仓库:https://github.com/mozilla-firefox/firefox,对应版本的标签为 FIREFOX_161_0_RELEASE。

git clone --depth 1 --branch FIREFOX_161_0_RELEASE https://github.com/mozilla-firefox/firefox.git
cd firefox
./mach bootstrap   # 选择 Firefox for Desktop

在 firefox/mozconfig 中写入:

mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/obj-release
ac_add_options --target=x86_64-pc-windows-msvc
ac_add_options --enable-optimize
ac_add_options --disable-debug
ac_add_options --disable-tests
ac_add_options --enable-release
ac_add_options --enable-update-channel=release

然后编译火狐,并从产物中导出 SDK,再用它编译原生库:

./mach build
cd <ruyiHttp>
python source/build.py --source-root <firefox> --nss-dist <firefox>/obj-release/dist --export-nss-sdk build/my-nss-sdk
python scripts/build_native.py --nss-dist build/my-nss-sdk

反馈与交流

欢迎通过 Issues 反馈问题或提出功能建议。 反馈时请附上 Python 版本、ruyihttp 版本、最小复现代码和错误信息,分享前移除 Cookie、Token 等私密数据。 微信联系Charleval。

相关项目:ruyiPage 提供 Firefox 浏览器自动化能力,适合需要页面操作的场景。

Metadata

Release files for ruyihttp 0.11.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for ruyihttp 0.11.0
File Interpreter ABI Platform Actions
ruyihttp-0.11.0-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for ruyihttp-0.11.0-py3-none-win_amd64.whl

Release files / ruyihttp-0.11.0-py3-none-win_amd64.whl

Download URL ruyihttp-0.11.0-py3-none-win_amd64.whl
Size 4.0 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
79eb2f08326c5f1e0a9ffddecef5136f45b50aefe952051f878e514a63939332
BLAKE2b-256 checksum
How to use checksums
f5765e918b50ec3021414d525993add261f54dafa107bc64128bdc3cf29d79b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Security for ruyihttp 0.11.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page