Debian Codegraph @

crt/aws-c-http/tests/mock_server/h2non_tls_server.py

raw symbols SCIP
1 # example server from http://python-hyper.org/projects/h2/en/stable/basic-usage.html
2
3 import json
4 import socket
5
6 import h2.connection
7 import h2.events
8 import h2.config
9
10
11 def send_response(conn, event):
12 stream_id = event.stream_id
13 response_data = b"success"
14
15 conn.send_headers(
16 stream_id=stream_id,
17 headers=[
18 (':status', '200'),
19 ],
20 )
21 conn.send_data(
22 stream_id=stream_id,
23 data=response_data,
24 end_stream=True
25 )
26
27
28 def handle(sock):
29 config = h2.config.H2Configuration(client_side=False)
30 conn = h2.connection.H2Connection(config=config)
31 conn.initiate_connection()
32 sock.sendall(conn.data_to_send())
33
34 while True:
35 data = sock.recv(65535)
36 if not data:
37 break
38
39 events = conn.receive_data(data)
40 for event in events:
41 if isinstance(event, h2.events.RequestReceived):
42 send_response(conn, event)
43
44 data_to_send = conn.data_to_send()
45 if data_to_send:
46 sock.sendall(data_to_send)
47
48
49 sock = socket.socket()
50 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
51 sock.bind(('0.0.0.0', 3280))
52 sock.listen(5)
53
54 while True:
55 handle(sock.accept()[0])