h11 has a fairly small public API, with all public symbols available directly at the top level:
In : import h11 In : h11.<TAB> h11.CLIENT h11.MUST_CLOSE h11.CLOSED h11.Paused h11.Connection h11.PRODUCT_ID h11.ConnectionClosed h11.ProtocolError h11.Data h11.Request h11.DONE h11.Response h11.EndOfMessage h11.SEND_BODY h11.ERROR h11.SEND_RESPONSE h11.IDLE h11.SERVER h11.InformationalResponse h11.SWITCHED_PROTOCOL h11.MIGHT_SWITCH_PROTOCOL
These symbols fall into three main categories: event classes, special
constants used to track different connection states, and the
Connection class itself. We’ll describe them in that order.
Events are the core of h11: the whole point of h11 is to let you think about HTTP transactions as being a series of events sent back and forth between a client and a server, instead of thinking in terms of bytes.
All events behave in essentially similar ways. Let’s take
Request as an example. Like all events, this is a “final”
class – you cannot subclass it. And like all events, it has several
Request, there are four of them:
b"1.1"; the rest have no default, so to create a
Request you have to specify their values:
In : req = h11.Request(method="GET", ...: target="/", ...: headers=[("Host", "example.com")]) ...:
Event constructors accept only keyword arguments, not positional arguments.
Events have a useful repr:
In : req Out: Request(method=b'GET', target=b'/', headers=[(b'host', b'example.com')], http_version=b'1.1')
And their fields are available as regular attributes:
In : req.method Out: b'GET' In : req.target