Metadata-Version: 2.4
Name: ngh2
Version: 0.6.1
Summary: A fast Sans-I/O HTTP/2 library for Python, powered by libnghttp2
Keywords: http,http2,nghttp2,protocol,sans-io
License-Expression: MIT
License-File: LICENSE
License-File: vendor/nghttp2/COPYING
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Python :: Free Threading :: 2 - Beta
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Typing :: Typed
Project-URL: Documentation, https://ngh2.readthedocs.io/
Project-URL: Changelog, https://github.com/cnzakii/ngh2/blob/main/CHANGELOG.md
Project-URL: Source, https://github.com/cnzakii/ngh2
Project-URL: Issues, https://github.com/cnzakii/ngh2/issues
Requires-Python: >=3.10
Description-Content-Type: text/markdown

<h1 align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://github.com/cnzakii/ngh2/raw/refs/heads/main/docs/overrides/assets/brand/ngh2-lockup-dark.svg">
    <img src="https://github.com/cnzakii/ngh2/raw/refs/heads/main/docs/overrides/assets/brand/ngh2-lockup-light.svg" height="56" alt="ngh2">
  </picture>
</h1>

<p align="center">
  <strong>A fast <a href="https://sans-io.readthedocs.io/">Sans-I/O</a> HTTP/2 library for Python, powered by <a href="https://nghttp2.org/documentation/">libnghttp2</a>.</strong>
</p>

<p align="center">
  <a href="https://github.com/cnzakii/ngh2/actions/workflows/ci.yml"><img src="https://github.com/cnzakii/ngh2/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
  <a href="https://codecov.io/gh/cnzakii/ngh2"><img src="https://codecov.io/gh/cnzakii/ngh2/graph/badge.svg" alt="codecov"></a>
  <a href="https://pypi.org/project/ngh2/"><img src="https://img.shields.io/pypi/v/ngh2.svg" alt="PyPI"></a>
  <a href="https://pypi.org/project/ngh2/"><img src="https://img.shields.io/pypi/pyversions/ngh2.svg" alt="Python versions"></a>
  <a href="https://github.com/cnzakii/ngh2/blob/main/pyproject.toml"><img src="https://img.shields.io/badge/free--threaded-3.14t%20%7C%203.15t-3776AB?logo=python&amp;logoColor=white" alt="Free-threaded CPython 3.14t and 3.15t"></a>
  <a href="https://github.com/cnzakii/ngh2/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License: MIT"></a>
</p>

Build HTTP/2 clients, servers, proxies, and protocol tools on the transport
and runtime you already use. ngh2 turns received bytes into events and your
calls into bytes to send; your code decides how and when those bytes move.

> ngh2 is currently alpha software. Its public API may change before the first
> stable release.

## Why ngh2

- **Fast.** ngh2 sets up connections, moves data, and completes requests
  14–30× faster than the pure-Python [h2](https://h2.readthedocs.io/). See
  [every measured scenario](https://github.com/cnzakii/ngh2#performance).
- **Proven protocol engine.** Framing, HPACK, flow control, and protocol
  validation come from [libnghttp2](https://nghttp2.org/documentation/), the
  HTTP/2 implementation behind curl, Node.js, and Apache httpd. CI also runs
  the [h2spec](https://github.com/summerwind/h2spec) conformance suite.
- **Fits any runtime.** ngh2 never opens sockets or starts an event loop, so
  the same code works with asyncio, threads, Trio, or in-memory tests.

## Installation

```console
python -m pip install ngh2
```

Or add it to a [uv](https://docs.astral.sh/uv/) project:

```console
uv add ngh2
```

ngh2 supports CPython 3.10 through 3.15, including free-threaded 3.14t and
3.15t. Wheels bundle libnghttp2 for Linux (glibc and musl), macOS, and
Windows, so no system library is needed.

## Example

A client and a server exchange one request in memory. A real program writes
`data_to_send()` to its socket and passes the bytes it reads to
`receive_data()`.

```python
import ngh2

client = ngh2.Connection(ngh2.Role.CLIENT)
server = ngh2.Connection(ngh2.Role.SERVER)
client.initiate_connection()
server.initiate_connection()

client.send_request(
    [
        (b":method", b"GET"),
        (b":scheme", b"https"),
        (b":authority", b"example.test"),
        (b":path", b"/"),
    ],
    end_stream=True,
)
server.receive_data(client.data_to_send())

for event in server.events():
    if isinstance(event, ngh2.RequestReceived):
        server.send_response(event.stream_id, [(b":status", b"200")])
        server.send_data(event.stream_id, b"hello", end_stream=True)
client.receive_data(server.data_to_send())

for event in client.events():
    print(event)
```

```text
SettingsReceived(settings=mappingproxy({}))
SettingsAcknowledged()
ResponseReceived(stream_id=1, headers=((b':status', b'200'),), end_stream=False)
DataReceived(stream_id=1, data=b'hello', end_stream=True)
StreamClosed(stream_id=1, error_code=<ErrorCode.NO_ERROR: 0>, local_error=None)
```

## What ngh2 handles

Each `Connection` manages one HTTP/2 connection and its multiplexed streams,
in the client or server role: request and response bodies, informational
responses, trailers, flow control, stream cancellation, and graceful shutdown.

Your application owns sockets, TLS and ALPN, buffering, connection pooling,
routing, retries, and timeouts. Calls are synchronous and never block on I/O.
Drive each `Connection` from one thread or task at a time; separate
connections can run concurrently.

## Documentation

- [First exchange](https://ngh2.readthedocs.io/learn/first-exchange/):
  start here. The tutorials go on to the protocol model and complete messages.
- [Guides](https://ngh2.readthedocs.io/guides/client/):
  drive asyncio clients and servers, manage flow control, and shut down
  cleanly.
- [HTTP/2 features](https://ngh2.readthedocs.io/features/settings-and-ping/):
  settings, server push, priorities, h2c, extended CONNECT, ALTSVC and ORIGIN.
- [API reference](https://ngh2.readthedocs.io/reference/):
  methods, accepted inputs, event fields, and exceptions.
- [Runnable examples](https://github.com/cnzakii/ngh2/tree/main/examples):
  complete programs used throughout the documentation.

## Performance

[`benchmarks/compare.py`][benchmark-script] runs a client and a server of the
same library in memory on one core, so the numbers are what the protocol work
costs, without sockets, TLS, or an event loop. Request rates and latency
follow gRPC's benchmarking guide: one request in flight, then 100 on one
connection, with P50 and P99 as nghttp2's h2load reports request time, and
one scenario with browser-sized headers.
Bulk transfer runs at the default and the maximum flow-control window.
Timing follows pyperf's guidance: 20 worker processes, calibrated loops, and
warm-ups. Work that h2 leaves to its caller, such as splitting bodies to the
window and acknowledging received data, is timed with it. Each table compares
one dimension; the last column is how many times better ngh2 does, or
h2 where it names h2.

<!-- benchmark:start -->

**Connections per second** (higher is better)

|  | ngh2 | h2 | ngh2 advantage |
|---|---:|---:|---:|
| HTTP/2 | 215,812 | 14,446 | 14.9× |

**Bulk transfer, Gbit/s** (higher is better)

|  | ngh2 | h2 | ngh2 advantage |
|---|---:|---:|---:|
| download, default window | 75.3 | 5.31 | 14.2× |
| upload, default window | 89.9 | 5.59 | 16.1× |
| download, maximum window | 104 | 6.53 | 16.0× |
| upload, maximum window | 104 | 6.17 | 16.8× |
| upload, 100 writes of 10 KiB | 91.3 | 4.39 | 20.8× |

**Requests per second** (higher is better)

|  | ngh2 | h2 | ngh2 advantage |
|---|---:|---:|---:|
| 1 in flight | 358,259 | 13,829 | 25.9× |
| 100 in flight | 490,104 | 19,185 | 25.5× |
| 1 in flight, many headers | 183,621 | 5,549 | 33.1× |

<details>
<summary>Request latency and memory</summary>

**Request latency, µs** (lower is better)

|  | ngh2 | h2 | ngh2 advantage |
|---|---:|---:|---:|
| P50, 1 in flight | 2.25 | 64.4 | 28.6× |
| P99, 1 in flight | 2.79 | 72.1 | 25.8× |
| P50, 100 in flight | 152 | 4,292 | 28.3× |
| P99, 100 in flight | 163 | 4,936 | 30.3× |
| P50, 1 in flight, many headers | 4.67 | 164 | 35.1× |
| P99, 1 in flight, many headers | 5.67 | 278 | 49.0× |

**Memory per idle connection, KiB** (lower is better)

|  | ngh2 | h2 | ngh2 advantage |
|---|---:|---:|---:|
| HTTP/2 | 22.4 | 18.7 | h2 1.2× |

</details>

_Environment: ngh2 0.6.0 · h2 4.4.1 · CPython 3.13.12 · Apple M4 · Darwin arm64 · 2026-11-29_

<!-- benchmark:end -->

These are protocol-layer measurements. End-to-end gains depend on how much of
your program's time goes to HTTP/2 processing rather than sockets, TLS, and
application work. libnghttp2 allocates a frame buffer of about 16 KiB when a
connection is created, so an idle connection costs more than with h2, and
sending needs no allocation later.

## Related projects

[h11r](https://github.com/cnzakii/h11r) (HTTP/1.1) and [ngquic](https://github.com/cnzakii/ngquic) (QUIC and HTTP/3)
are the other Sans-I/O protocol libraries for Python in this family.

## Contributing

See [CONTRIBUTING.md][contributing-guide] for development, testing, and release
guidance.

## License

ngh2 is MIT licensed. Distributed wheels also contain libnghttp2 under its MIT
license; both license texts are included in every distribution.

[benchmark-script]: https://github.com/cnzakii/ngh2/blob/main/benchmarks/compare.py
[contributing-guide]: https://github.com/cnzakii/ngh2/blob/main/CONTRIBUTING.md
