6 nghttp2 offers some high level Python API to C library. The bindings
7 currently provide HPACK compressor and decompressor classes and HTTP/2
10 The extension module is called ``nghttp2``.
12 ``make`` will build the bindings. The target Python version is
13 determined by configure script. If the detected Python version is not
14 what you expect, specify a path to Python executable in ``PYTHON``
15 variable as an argument to configure script (e.g., ``./configure
16 PYTHON=/usr/bin/python3.5``).
21 .. py:class:: HDDeflater(hd_table_bufsize_max=DEFLATE_MAX_HEADER_TABLE_SIZE)
23 This class is used to perform header compression. The
24 *hd_table_bufsize_max* limits the usage of header table in the
25 given amount of bytes. The default value is
26 :py:data:`DEFLATE_MAX_HEADER_TABLE_SIZE`. This is necessary
27 because the deflater and inflater share the same amount of header
28 table and the inflater decides that number. The deflater may not
29 want to use all header table size because of limited memory
30 availability. In that case, *hd_table_bufsize_max* can be used to
31 cap the upper limit of table size whatever the header table size is
32 chosen by the inflater.
34 .. py:method:: deflate(headers)
36 Deflates the *headers*. The *headers* must be sequence of tuple
37 of name/value pair, which are byte strings (not unicode string).
39 This method returns the deflated header block in byte string.
40 Raises the exception if any error occurs.
42 .. py:method:: set_no_refset(no_refset)
44 Tells the deflater not to use reference set if *no_refset* is
45 evaluated to ``True``. If that happens, on each subsequent
46 invocation of :py:meth:`deflate()`, deflater will clear up
49 .. py:method:: change_table_size(hd_table_bufsize_max)
51 Changes header table size to *hd_table_bufsize_max* byte. if
52 *hd_table_bufsize_max* is strictly larger than
53 ``hd_table_bufsize_max`` given in constructor,
54 ``hd_table_bufsize_max`` is used as header table size instead.
56 Raises the exception if any error occurs.
58 .. py:method:: get_hd_table()
60 Returns copy of current dynamic header table.
62 The following example shows how to deflate header name/value pairs:
64 .. code-block:: python
66 import binascii, nghttp2
68 deflater = nghttp2.HDDeflater()
70 res = deflater.deflate([(b'foo', b'bar'),
73 print(binascii.b2a_hex(res))
76 .. py:class:: HDInflater()
78 This class is used to perform header decompression.
80 .. py:method:: inflate(data)
82 Inflates the deflated header block *data*. The *data* must be
85 Raises the exception if any error occurs.
87 .. py:method:: change_table_size(hd_table_bufsize_max)
89 Changes header table size to *hd_table_bufsize_max* byte.
91 Raises the exception if any error occurs.
93 .. py:method:: get_hd_table()
95 Returns copy of current dynamic header table.
97 The following example shows how to inflate deflated header block:
99 .. code-block:: python
101 deflater = nghttp2.HDDeflater()
103 data = deflater.deflate([(b'foo', b'bar'),
106 inflater = nghttp2.HDInflater()
108 hdrs = inflater.inflate(data)
113 .. py:function:: print_hd_table(hdtable)
115 Convenient function to print *hdtable* to the standard output. The
116 *hdtable* is the one retrieved by
117 :py:meth:`HDDeflater.get_hd_table()` or
118 :py:meth:`HDInflater.get_hd_table()`. This function does not work
119 if header name/value cannot be decoded using UTF-8 encoding.
121 In output, ``s=N`` means the entry occupies ``N`` bytes in header
122 table. If ``r=y``, then the entry is in the reference set.
124 .. py:data:: DEFAULT_HEADER_TABLE_SIZE
126 The default header table size, which is 4096 as per HTTP/2
129 .. py:data:: DEFLATE_MAX_HEADER_TABLE_SIZE
131 The default header table size for deflater. The initial value
139 We use :py:mod:`asyncio` for HTTP/2 server classes, and ALPN.
140 Therefore, Python 3.5 or later is required to use these objects.
141 To explicitly configure nghttp2 build to use Python 3.5, specify
142 the ``PYTHON`` variable to the path to Python 3.5 executable when
143 invoking configure script like this:
147 $ ./configure PYTHON=/usr/bin/python3.5
149 .. py:class:: HTTP2Server(address, RequestHandlerClass, ssl=None)
151 This class builds on top of the :py:mod:`asyncio` event loop. On
152 construction, *RequestHandlerClass* must be given, which must be a
153 subclass of :py:class:`BaseRequestHandler` class.
155 The *address* must be a tuple of hostname/IP address and port to
156 bind. If hostname/IP address is ``None``, all interfaces are
159 To enable SSL/TLS, specify instance of :py:class:`ssl.SSLContext`
160 in *ssl*. Before passing *ssl* to
161 :py:func:`BaseEventLoop.create_server`, ALPN protocol identifiers
162 are set using :py:meth:`ssl.SSLContext.set_npn_protocols`.
164 To disable SSL/TLS, omit *ssl* or specify ``None``.
166 .. py:method:: serve_forever()
168 Runs server and processes incoming requests forever.
170 .. py:class:: BaseRequestHandler(http2, stream_id)
172 The class is used to handle the single HTTP/2 stream. By default,
173 it does not nothing. It must be subclassed to handle each event
176 The first callback method invoked is :py:meth:`on_headers()`. It is
177 called when HEADERS frame, which includes request header fields, is
180 If request has request body, :py:meth:`on_data()` is invoked for
181 each chunk of received data chunk.
183 When whole request is received, :py:meth:`on_request_done()` is
186 When stream is closed, :py:meth:`on_close()` is called.
188 The application can send response using :py:meth:`send_response()`
189 method. It can be used in :py:meth:`on_headers()`,
190 :py:meth:`on_data()` or :py:meth:`on_request_done()`.
192 The application can push resource using :py:meth:`push()` method.
193 It must be used before :py:meth:`send_response()` call.
195 A :py:class:`BaseRequestHandler` has the following instance
198 .. py:attribute:: client_address
200 Contains a tuple of the form ``(host, port)`` referring to the
203 .. py:attribute:: stream_id
205 Stream ID of this stream
207 .. py:attribute:: scheme
209 Scheme of the request URI. This is a value of ``:scheme``
212 .. py:attribute:: method
214 Method of this stream. This is a value of ``:method`` header
217 .. py:attribute:: host
219 This is a value of ``:authority`` or ``host`` header field.
221 .. py:attribute:: path
223 This is a value of ``:path`` header field.
225 .. py:attribute:: headers
227 Request header fields.
229 A :py:class:`BaseRequestHandler` has the following methods:
231 .. py:method:: on_headers()
233 Called when request HEADERS is arrived. By default, this method
236 .. py:method:: on_data(data)
238 Called when a chunk of request body *data* is arrived. This
239 method will be called multiple times until all data are
240 received. By default, this method does nothing.
242 .. py:method:: on_request_done()
244 Called when whole request was received. By default, this method
247 .. py:method:: on_close(error_code)
249 Called when stream is about to close. The *error_code*
250 indicates the reason of closure. If it is ``0``, the stream is
251 going to close without error.
253 .. py:method:: send_response(status=200, headers=None, body=None)
255 Send response. The *status* is HTTP status code. The *headers*
256 is additional response headers. The *:status* header field will
257 be appended by the library. The *body* is the response body.
258 It could be ``None`` if response body is empty. Or it must be
259 instance of either ``str``, ``bytes``, :py:class:`io.IOBase` or
260 callable, called body generator, which takes one parameter,
261 size. The body generator generates response body. It can pause
262 generation of response so that it can wait for slow backend data
263 generation. When invoked, it should return tuple, byte string
264 at most size length and flag. The flag is either
265 :py:data:`DATA_OK`, :py:data:`DATA_EOF` or
266 :py:data:`DATA_DEFERRED`. For non-empty byte string and it is
267 not the last chunk of response, :py:data:`DATA_OK` must be
268 returned as flag. If this is the last chunk of the response
269 (byte string could be ``None``), :py:data:`DATA_EOF` must be
270 returned as flag. If there is no data available right now, but
271 additional data are anticipated, return tuple (``None``,
272 :py:data:`DATA_DEFERRED`). When data arrived, call
273 :py:meth:`resume()` and restart response body transmission.
275 Only the body generator can pause response body generation;
276 instance of :py:class:`io.IOBase` must not block.
278 If instance of ``str`` is specified as *body*, it will be
281 The *headers* is a list of tuple of the form ``(name,
282 value)``. The ``name`` and ``value`` can be either byte string
283 or unicode string. In the latter case, they will be encoded
286 Raises the exception if any error occurs.
288 .. py:method:: push(path, method='GET', request_headers=None, status=200, headers=None, body=None)
290 Push a specified resource. The *path* is a path portion of
291 request URI for this resource. The *method* is a method to
292 access this resource. The *request_headers* is additional
293 request headers to access this resource. The ``:scheme``,
294 ``:method``, ``:authority`` and ``:path`` are appended by the
295 library. The ``:scheme`` and ``:authority`` are inherited from
296 request header fields of the associated stream.
298 The *status* is HTTP status code. The *headers* is additional
299 response headers. The ``:status`` header field is appended by
300 the library. The *body* is the response body. It has the same
301 semantics of *body* parameter of :py:meth:`send_response()`.
303 The headers and request_headers are a list of tuple of the form
304 ``(name, value)``. The ``name`` and ``value`` can be either byte
305 string or unicode string. In the latter case, they will be
308 Returns an instance of ``RequestHandlerClass`` specified in
309 :py:class:`HTTP2Server` constructor for the pushed resource.
311 Raises the exception if any error occurs.
313 .. py:method:: resume()
315 Signals the restarting of response body transmission paused by
316 ``DATA_DEFERRED`` from the body generator (see
317 :py:meth:`send_response()` about the body generator). It is not
318 an error calling this method while response body transmission is
323 ``DATA_OK`` indicates non empty data is generated from body generator.
325 .. py:data:: DATA_EOF
327 ``DATA_EOF`` indicates the end of response body.
329 .. py:data:: DATA_DEFERRED
331 ``DATA_DEFERRED`` indicates that data are not available right now
332 and response should be paused.
334 The following example illustrates :py:class:`HTTP2Server` and
335 :py:class:`BaseRequestHandler` usage:
337 .. code-block:: python
339 #!/usr/bin/env python
345 class Handler(nghttp2.BaseRequestHandler):
347 def on_headers(self):
348 self.push(path='/css/style.css',
349 request_headers = [('content-type', 'text/css')],
351 body='body{margin:0;}')
353 self.send_response(status=200,
354 headers = [('content-type', 'text/plain')],
355 body=io.BytesIO(b'nghttp2-python FTW'))
357 ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
358 ctx.options = ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3
359 ctx.load_cert_chain('server.crt', 'server.key')
361 # give None to ssl to make the server non-SSL/TLS
362 server = nghttp2.HTTP2Server(('127.0.0.1', 8443), Handler, ssl=ctx)
363 server.serve_forever()
365 The following example illustrates HTTP/2 server using asynchronous
366 response body generation. This is simplified reverse proxy:
368 .. code-block:: python
370 #!/usr/bin/env python
381 def get_http_header(handler, url):
382 url = urllib.parse.urlsplit(url)
383 ssl = url.scheme == 'https'
385 if url.scheme == 'https':
392 connect = asyncio.open_connection(url.hostname, port, ssl=ssl)
393 reader, writer = yield from connect
394 req = 'GET {path} HTTP/1.0\r\n\r\n'.format(path=url.path or '/')
395 writer.write(req.encode('utf-8'))
396 # skip response header fields
398 line = yield from reader.readline()
404 b = yield from reader.read(4096)
414 def __init__(self, handler):
415 self.handler = handler
416 self.handler.eof = False
417 self.handler.buf = io.BytesIO()
419 def generate(self, n):
420 buf = self.handler.buf
422 if not data and not self.handler.eof:
423 return None, nghttp2.DATA_DEFERRED
424 return data, nghttp2.DATA_EOF if self.handler.eof else nghttp2.DATA_OK
426 class Handler(nghttp2.BaseRequestHandler):
428 def on_headers(self):
430 asyncio.async(get_http_header(
431 self, 'http://localhost' + self.path.decode('utf-8')))
432 self.send_response(status=200, body=body.generate)
434 ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
435 ctx.options = ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3
436 ctx.load_cert_chain('server.crt', 'server.key')
438 server = nghttp2.HTTP2Server(('127.0.0.1', 8443), Handler, ssl=ctx)
439 server.serve_forever()