-
Notifications
You must be signed in to change notification settings - Fork 4
/
Basic_aioquic_server.py
59 lines (42 loc) · 1.52 KB
/
Basic_aioquic_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
######################BASIC SERVER ####################
import argparse
import asyncio
import struct
import aioquic.asyncio
from aioquic.quic.configuration import QuicConfiguration
from aioquic.quic.events import StreamDataReceived
_I = struct.Struct('!I')
class QuicServer(aioquic.asyncio.QuicConnectionProtocol):
def quic_event_received(self, quic_event):
if isinstance(quic_event, StreamDataReceived):
size, = _I.unpack(quic_event.data)
self._quic.send_stream_data(
stream_id=quic_event.stream_id,
data=b'\x00' * size,
end_stream=True,
)
self.transmit()
async def main(certfile, keyfile=None, password=None, host='172.16.2.1', port=9999):
configuration = QuicConfiguration(is_client=False)
configuration.load_cert_chain(certfile=certfile, keyfile=keyfile, password=password)
server = await aioquic.asyncio.serve(
host, port,
configuration=configuration,
create_protocol=QuicServer,
)
loop = asyncio.get_running_loop()
try:
await loop.create_future()
finally:
server.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--certificate', required=True)
parser.add_argument('-k', '--private-key')
args = parser.parse_args()
asyncio.run(
main(
certfile=args.certificate,
keyfile=args.private_key,
)
)