Начальный комит

bare
inpos 2016-05-30 14:17:04 +03:00
parent 7de6ed476a
commit 568d49d644
4 changed files with 67 additions and 31 deletions

View File

29
handlers/smtp.py 100644
View File

@ -0,0 +1,29 @@
from socketserver import StreamRequestHandler
ERR_CODE = {
220 : ''
500 : 'Syntax error, command unrecognized',
}
class SmtpProcessor(object):
def __init__(self, src_addr, rfile,wfile):
self.src_addr = src_addr
self.rfile = rfile
self.wfile = wfile
self.unrec_commands_count = 0
class SmtpHandler(StreamRequestHandler):
def handle(self):
# self.rfile is a file-like object created by the handler;
# we can now use e.g. readline() instead of raw recv() calls
self.data = self.rfile.readline().strip()
print("{} wrote:".format(self.client_address[0]))
print(self.data)
# Likewise, self.wfile is a file-like object used to write back
# to the client
self.wfile.write(self.data.upper())
#######################################################
processor = SmtpProcessor(self.client_address, self.rfile, self.wfile)

55
main.py
View File

@ -1,31 +1,38 @@
import asyncio import socketserver, subprocess, sys
from threading import Thread
from pprint import pprint
import json
clients = {} # task -> (reader, writer) my_unix_command = ['bc']
HOST = 'localhost'
PORT = 2000
def client_connected_handler(client_reader, client_writer): class SingleTCPHandler(socketserver.StreamRequestHandler):
# 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): def handle(self):
# When the tasks that handles the specific client connection is done # self.rfile is a file-like object created by the handler;
del clients[task] # we can now use e.g. readline() instead of raw recv() calls
self.data = self.rfile.readline().strip()
print("{} wrote:".format(self.client_address[0]))
print(self.data)
# Likewise, self.wfile is a file-like object used to write back
# to the client
self.wfile.write(self.data.upper())
# Add the client_done callback to be run when the future becomes done
task.add_done_callback(client_done)
@asyncio.coroutine class SimpleServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
def handle_client(client_reader, client_writer): # Ctrl-C will cleanly kill all spawned threads
# Handle the requests for a specific client with a line oriented protocol daemon_threads = True
while True: # much faster rebinding
# Read a line allow_reuse_address = True
data = (yield from client_reader.readline())
# Send it back to the client
client_writer.write(data)
loop = asyncio.get_event_loop() def __init__(self, server_address, RequestHandlerClass):
server = loop.run_until_complete(asyncio.start_server(client_connected_handler, 'localhost', 2222)) socketserver.TCPServer.__init__(self, server_address, RequestHandlerClass)
if __name__ == "__main__":
server = SimpleServer((HOST, PORT), SingleTCPHandler)
# terminate with Ctrl-C
try: try:
loop.run_forever() server.serve_forever()
finally: except KeyboardInterrupt:
loop.close() sys.exit(0)

View File