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

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)

57
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):
# 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)
class SingleTCPHandler(socketserver.StreamRequestHandler):
def client_done(task):
# When the tasks that handles the specific client connection is done
del clients[task]
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())
# 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)
class SimpleServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
# Ctrl-C will cleanly kill all spawned threads
daemon_threads = True
# much faster rebinding
allow_reuse_address = True
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()
def __init__(self, server_address, RequestHandlerClass):
socketserver.TCPServer.__init__(self, server_address, RequestHandlerClass)
if __name__ == "__main__":
server = SimpleServer((HOST, PORT), SingleTCPHandler)
# terminate with Ctrl-C
try:
server.serve_forever()
except KeyboardInterrupt:
sys.exit(0)

View File