```httpx2```
HTTPX2 – A next-generation HTTP client for Python

原始链接: https://github.com/pydantic/httpx2

HTTPX2 是一款功能丰富的新一代 Python HTTP 客户端,旨在作为原 HTTPX 项目的稳定且持续维护的继任者。在 Pydantic 的管理下,HTTPX2 在保持原项目设计理念的同时,确保了生产系统的持续安全更新与可靠性。 主要特性包括: * **多功能性**:支持同步与异步 API,以及 HTTP/1.1 和 HTTP/2 协议。 * **兼容性**:基于 `requests` 库易用的特性构建,涵盖了连接池、会话保持和代理支持等标准功能。 * **集成工具**:包含可选的命令行客户端,并与 Pydantic Logfire 无缝集成,通过 OpenTelemetry 提供全面的可观测性。 * **稳健基础**:基于 `anyio` 和 `httpcore2` 等高质量库构建,提供现代且高性能的网络体验。 安装可通过 pip 轻松完成(`pip install httpx2`),并提供针对 HTTP/2、SOCKS 代理和命令行工具的可选安装包。完整的文档和快速入门指南请访问 https://httpx2.pydantic.dev/。本项目沿用 BSD 许可证,秉承前作精神,继续作为 Python 生态系统中的关键组件发挥作用。

关于 **HTTPX2**(流行 Python HTTP 客户端 `httpx` 的一个分支)发布的讨论,凸显了 Python 生态系统中持续存在的分裂问题。 用户质疑在已有 `requests` 和 `httpx` 的情况下,为何还要不断涌现新的库。普遍的共识是,库的“更迭”是由不断演进的技术需求所驱动的,例如对 `async`(异步)支持、新协议(HTTP/2 和 HTTP/3)以及不同性能需求的需求。 原有的 `httpx` 项目因维护者停止接受贡献而陷入停滞,从而催生了对分支版本的需求。虽然存在 `aiohttp` 或 `niquests` 等替代方案,但它们在 API 设计、性能和功能支持方面各有权衡。 许多评论者对“HTTPX2”这个命名表示不满,担心它会与 HTTP/2 协议混淆。另一些人则指出,新工具的不断涌现反映了人们正在寻找一种能在易用性、现代功能和性能之间取得平衡的“完美”实现。归根结底,库的激增凸显了在快速变化的环境中维护长期、功能完备的项目十分困难,这往往导致社区只能依赖由社区主导的分支或替代品。
相关文章

原文

A next-generation HTTP client for Python.

Test Suite Package version Join Slack

HTTPX2 is a fully featured HTTP client library for Python. It includes an integrated command line client, has support for both HTTP/1.1 and HTTP/2, and provides both sync and async APIs.

Note

HTTPX2 is a continuation of the wonderful work started by @lovelydinosaur and the broader HTTPX community. We're enormously grateful for everything that has gone into HTTPX over the years - it has been a foundational piece of the modern Python ecosystem, and this project would not exist without it.

With HTTPX itself seeing limited activity recently, Pydantic is picking up stewardship under the HTTPX2 name so that users have a reliably maintained path forward - including timely security updates for a library that sits in the critical path of so many production systems. Our aim is to honour the original project's design, keep it stable for everyone relying on it, and continue evolving it carefully. Thank you to @lovelydinosaur and every past contributor for laying such a strong foundation. 💙


Install HTTPX2 using pip:

Now, let's get started:

>>> import httpx2
>>> r = httpx2.get('https://www.example.org/')
>>> r
<Response [200 OK]>
>>> r.status_code
200
>>> r.headers['content-type']
'text/html; charset=UTF-8'
>>> r.text
'<!doctype html>\n<html>\n<head>\n<title>Example Domain</title>...'

Or, using the command-line client.

pip install 'httpx2[cli]'  # The command line client is an optional dependency.

Which now allows us to use HTTPX2 directly from the command-line:

HTTPX2 builds on the well-established usability of requests, and gives you:

Plus all the standard features of requests...

  • International Domains and URLs
  • Keep-Alive & Connection Pooling
  • Sessions with Cookie Persistence
  • Browser-style SSL Verification
  • Basic/Digest Authentication
  • Elegant Key/Value Cookies
  • Automatic Decompression
  • Automatic Content Decoding
  • Unicode Response Bodies
  • Multipart File Uploads
  • HTTP(S) Proxy Support
  • Connection Timeouts
  • Streaming Downloads
  • .netrc Support
  • Chunked Requests

Install with pip:

Or, to include the optional HTTP/2 support, use:

pip install httpx2[http2]

Project documentation is available at https://httpx2.pydantic.dev/.

For a run-through of all the basics, head over to the QuickStart.

For more advanced topics, see the Advanced Usage section, the async support section, or the HTTP/2 section.

The Developer Interface provides a comprehensive API reference.

To find out about tools that integrate with HTTPX, see Third Party Packages.

Observability with Pydantic Logfire

Install Logfire with HTTPX instrumentation support:

pip install 'logfire[httpx]'

HTTPX2 works out of the box with Pydantic Logfire, our observability platform built on OpenTelemetry. One line instruments every request, giving you traces, timings, and status codes with no other changes:

import logfire
import httpx2

logfire.configure()
logfire.instrument_httpx()

httpx2.get("https://pydantic.dev/")

This works the same way for explicit httpx2.Client() and httpx2.AsyncClient() instances, and you can scope instrumentation to a single client by passing it in: logfire.instrument_httpx(client). Pass capture_all=True to also record headers and bodies. See the Logfire HTTPX docs for the full set of options.

If you want to contribute with HTTPX2 check out the Contributing Guide to learn how to start.

The HTTPX2 project relies on these excellent libraries:

  • httpcore2 - The underlying transport implementation for httpx2.
  • anyio - Structured concurrency primitives, used to support both asyncio and trio.
  • truststore - SSL verification using the operating system's trust store.
  • idna - Internationalized domain name support.

As well as these optional installs:

  • h2 - HTTP/2 support. (Optional, with httpx2[http2])
  • socksio - SOCKS proxy support. (Optional, with httpx2[socks])
  • rich - Rich terminal support. (Optional, with httpx2[cli])
  • click - Command line client support. (Optional, with httpx2[cli])
  • brotli or brotlicffi - Decoding for "brotli" compressed responses. (Optional, with httpx2[brotli])
  • backports.zstd - Decoding for "zstd" compressed responses on Python 3.13 and below. (Optional, with httpx2[zstd]. On Python 3.14+, zstd is supported via the stdlib compression.zstd module when it is available, and the httpx2[zstd] extra installs nothing.)

A huge amount of credit is due to requests for the API layout that much of this work follows, as well as to urllib3 for plenty of design inspiration around the lower-level networking details.


HTTPX2 is BSD licensed code.
Designed & crafted with care.

— 🦋 —

联系我们 contact @ memedata.com