From 7de6ed476a421ffe12d91d6e1112eb9ea0c9a50d Mon Sep 17 00:00:00 2001 From: inpos Date: Mon, 30 May 2016 09:08:13 +0300 Subject: [PATCH] =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2=D1=8B?= =?UTF-8?q?=D0=B9=20=D0=BA=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 31 +++++++++++++++++++++++++++++++ mechs/smtp.py | 0 2 files changed, 31 insertions(+) create mode 100644 main.py create mode 100644 mechs/smtp.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..e81f4b3 --- /dev/null +++ b/main.py @@ -0,0 +1,31 @@ +import asyncio + +clients = {} # task -> (reader, writer) + +def client_connected_handler(client_reader, client_writer): + # Start a new asyncio.Task to handle this specific client connection + task = asyncio.Task(handle_client(client_reader, client_writer)) + clients[task] = (client_reader, client_writer) + + def client_done(task): + # When the tasks that handles the specific client connection is done + del clients[task] + + # Add the client_done callback to be run when the future becomes done + task.add_done_callback(client_done) + +@asyncio.coroutine +def handle_client(client_reader, client_writer): + # Handle the requests for a specific client with a line oriented protocol + while True: + # Read a line + data = (yield from client_reader.readline()) + # Send it back to the client + client_writer.write(data) + +loop = asyncio.get_event_loop() +server = loop.run_until_complete(asyncio.start_server(client_connected_handler, 'localhost', 2222)) +try: + loop.run_forever() +finally: + loop.close() \ No newline at end of file diff --git a/mechs/smtp.py b/mechs/smtp.py new file mode 100644 index 0000000..e69de29