echo.py 983 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # this thigng works, but idc.py drops incomingmessages
  2. import trio
  3. from itertools import count
  4. import ssl
  5. PORT = 12345
  6. CONNECTION_COUNTER = count()
  7. ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
  8. ctx.load_cert_chain(
  9. "/etc/letsencrypt/live/fcm.andrewyu.org/fullchain.pem",
  10. "/etc/letsencrypt/live/fcm.andrewyu.org/privkey.pem",
  11. )
  12. async def echo_server(server_stream):
  13. ident = next(CONNECTION_COUNTER)
  14. print(f"echo_server {ident}: started")
  15. try:
  16. async for data in server_stream:
  17. print(f"echo_server {ident}: received data {data!r}")
  18. await server_stream.send_all(data)
  19. print(f"echo_server {ident}: connection closed")
  20. except Exception as exc:
  21. print(f"echo_server {ident}: crashed: {exc!r}")
  22. async def tls_wrapper(server_stream):
  23. return await echo_server(
  24. trio.SSLStream(server_stream, ctx, server_side=True)
  25. )
  26. async def main():
  27. await trio.serve_tcp(tls_wrapper, PORT)
  28. trio.run(main)