aboutsummaryrefslogtreecommitdiff
path: root/echo.py
blob: a7a65b7ea5b7caa829b34469cdaccb660db5b73b (plain) (blame)
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
# this thigng works, but idc.py drops incomingmessages
import trio
from itertools import count
import ssl


PORT = 12345

CONNECTION_COUNTER = count()


ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ctx.load_cert_chain(
    "/etc/letsencrypt/live/fcm.andrewyu.org/fullchain.pem",
    "/etc/letsencrypt/live/fcm.andrewyu.org/privkey.pem",
)


async def echo_server(server_stream):
    ident = next(CONNECTION_COUNTER)
    print(f"echo_server {ident}: started")
    try:
        async for data in server_stream:
            print(f"echo_server {ident}: received data {data!r}")
            await server_stream.send_all(data)
        print(f"echo_server {ident}: connection closed")
    except Exception as exc:
        print(f"echo_server {ident}: crashed: {exc!r}")


async def tls_wrapper(server_stream):
    return await echo_server(
        trio.SSLStream(server_stream, ctx, server_side=True)
    )


async def main():
    await trio.serve_tcp(tls_wrapper, PORT)


trio.run(main)