h11: A pure-Python HTTP/1.1 protocol library¶
h11 is an HTTP/1.1 protocol library written in Python, heavily inspired by hyper-h2.
h11’s goal is to be a simple, robust, complete, and non-hacky implementation of the first “chapter” of the HTTP/1.1 spec: RFC 7230: HTTP/1.1 Message Syntax and Routing. That is, it mostly focuses on implementing HTTP at the level of taking bytes on and off the wire, and the headers related to that, and tries to be picky about spec conformance when possible. It doesn’t know about higher-level concerns like URL routing, conditional GETs, cross-origin cookie policies, or content negotiation. But it does know how to take care of framing, cross-version differences in keep-alive handling, and the “obsolete line folding” rule, and to use bounded time and space to process even pathological / malicious input, so that you can focus your energies on the hard / interesting parts for your application. And it tries to support the full specification in the sense that any useful HTTP/1.1 conformant application should be able to use h11.
This is a “bring-your-own-I/O” protocol library; like h2, it contains no I/O code whatsoever. This means you can hook h11 up to your favorite network API, and that could be anything you want: synchronous, threaded, asynchronous, or your own implementation of RFC 6214 – h11 won’t judge you. This is h11’s main feature compared to the current state of the art, where every HTTP library is tightly bound to a particular network framework, and every time a new network API comes along then someone has to start over reimplementing the entire HTTP stack from scratch. We highly recommend Cory Benfield’s excellent blog post about the advantages of this approach.
This also means that h11 is not immediately useful out of the box:
it’s a toolkit for building programs that speak HTTP, not something
that could directly replace
whatever. But h11 makes it much easier to implement something like
Requirements: Python 3.7+ (PyPy works great)
The last Python 2-compatible version was h11 0.11.x.
pip install h11
Sources and bug tracker: https://github.com/python-hyper/h11
Code of conduct: Contributors are requested to follow our code of conduct in all project spaces.
- Getting started: Writing your own HTTP/1.1 client
- API documentation
- The state machine
- Special constants
- The Connection object
- Error handling
- Message body framing:
Content-Lengthand all that
- Re-using a connection: keep-alive and pipelining
- Flow control
- Closing connections
- Switching protocols
- Support for
- Identifying h11 in requests and responses
- Chunked Transfer Encoding Delimiters
- Details of our HTTP support for HTTP nerds
- History of changes