Author: Kyle Fuller <kyle@fuller.li> 2024-06-01 13:49:13 +0100 +0100
Committer: Kyle Fuller <kyle@fuller.li> 2024-06-01 13:49:13 +0100 +0100
Commit: d5ae9c237f37fc5eea870e69cee842f903f15772
Parent: 3b69f1e1c5dbbf5b0fbad625609941b874948c35
feat(vsnip): add python example for asyncio server
diff --git a/.vsnip/python.json b/.vsnip/python.json
index 739f3b208647c0b094c8c986c193f04f1838445e..8ef281ea85160f5b9e7886c80cb3e03873b50952 100644
--- a/.vsnip/python.json
+++ b/.vsnip/python.json
@@ -5,5 +5,31 @@ "body": [
"if __name__ == '__main__':",
" ${0:pass}"
]
+ },
+ "asyncio server": {
+ "prefix": "asyncserver",
+ "body": [
+ "import asyncio",
+ "",
+ "",
+ "async def handle_connection(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:",
+ " while True:",
+ " line = await reader.readuntil(b'\\r\\n')",
+ " print(line.strip().decode('utf-8'))",
+ " writer.write(line)",
+ " await writer.drain()",
+ "",
+ "",
+ "async def main() -> None:",
+ " server = await asyncio.start_server(handle_connection, 'localhost', 9000)",
+ "",
+ " async with server:",
+ " print('Listening on localhost:9000')",
+ " await server.serve_forever()",
+ "",
+ "",
+ "if __name__ == '__main__':",
+ " asyncio.run(main())"
+ ]
}
}