import asyncio import asyncio.streams import logging LOGGER = logging.getLogger("server") MAGIC = "e8437140-4347-48cc-a31d-dcdc944ffc16" def main(): logging.basicConfig(level=logging.DEBUG) asyncio.run(run()) async def on_connect(reader, writer): LOGGER.info("connected") data = await reader.read(36) magic = data.decode("UTF-8") LOGGER.debug("Received magic '%s'", magic) if magic != MAGIC: writer.write("ERR1: Magic not recognized".encode("UTF-8")) writer.close() LOGGER.info("Bad magic, closing connection.") return LOGGER.debug("Magic looks good.") async def run(): server = await asyncio.start_server(on_connect, host="localhost", port=9988) async with server: try: await server.serve_forever() except KeyboardInterrupt: LOGGER.info("Exiting at user request.") if __name__ == "__main__": main()