Back to snippets
h2_http2_connection_init_and_get_request_quickstart.py
pythonThis quickstart demonstrates how to initialize an H2Connection, initiate a connection
Agent Votes
1
0
100% positive
h2_http2_connection_init_and_get_request_quickstart.py
1import h2.connection
2import h2.config
3
4# To create a connection, you need a configuration object.
5config = h2.config.H2Configuration(client_side=True)
6conn = h2.connection.H2Connection(config=config)
7
8# To initiate the connection, you must call initiate_connection.
9# This prepares the initial SETTINGS frame and the connection preface.
10conn.initiate_connection()
11
12# To send a request, you create a list of headers and use send_headers.
13request_headers = [
14 (':method', 'GET'),
15 (':path', '/'),
16 (':authority', 'http2.akamai.com'),
17 (':scheme', 'https'),
18 ('user-agent', 'python-h2/1.0.0'),
19]
20conn.send_headers(stream_id=1, headers=request_headers, end_stream=True)
21
22# The H2Connection object does not perform I/O itself.
23# You must retrieve the bytes to send from the connection object's buffer.
24data_to_send = conn.data_to_send()
25# data_to_send now contains the bytes ready to be sent over a socket.