Начальный комит
parent
7de6ed476a
commit
568d49d644
|
@ -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)
|
||||||
|
|
69
main.py
69
main.py
|
@ -1,31 +1,38 @@
|
||||||
import asyncio
|
import socketserver, subprocess, sys
|
||||||
|
from threading import Thread
|
||||||
clients = {} # task -> (reader, writer)
|
from pprint import pprint
|
||||||
|
import json
|
||||||
def client_connected_handler(client_reader, client_writer):
|
|
||||||
# Start a new asyncio.Task to handle this specific client connection
|
my_unix_command = ['bc']
|
||||||
task = asyncio.Task(handle_client(client_reader, client_writer))
|
HOST = 'localhost'
|
||||||
clients[task] = (client_reader, client_writer)
|
PORT = 2000
|
||||||
|
|
||||||
def client_done(task):
|
class SingleTCPHandler(socketserver.StreamRequestHandler):
|
||||||
# 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;
|
||||||
# Add the client_done callback to be run when the future becomes done
|
# we can now use e.g. readline() instead of raw recv() calls
|
||||||
task.add_done_callback(client_done)
|
self.data = self.rfile.readline().strip()
|
||||||
|
print("{} wrote:".format(self.client_address[0]))
|
||||||
@asyncio.coroutine
|
print(self.data)
|
||||||
def handle_client(client_reader, client_writer):
|
# Likewise, self.wfile is a file-like object used to write back
|
||||||
# Handle the requests for a specific client with a line oriented protocol
|
# to the client
|
||||||
while True:
|
self.wfile.write(self.data.upper())
|
||||||
# Read a line
|
|
||||||
data = (yield from client_reader.readline())
|
|
||||||
# Send it back to the client
|
class SimpleServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
client_writer.write(data)
|
# Ctrl-C will cleanly kill all spawned threads
|
||||||
|
daemon_threads = True
|
||||||
loop = asyncio.get_event_loop()
|
# much faster rebinding
|
||||||
server = loop.run_until_complete(asyncio.start_server(client_connected_handler, 'localhost', 2222))
|
allow_reuse_address = True
|
||||||
try:
|
|
||||||
loop.run_forever()
|
def __init__(self, server_address, RequestHandlerClass):
|
||||||
finally:
|
socketserver.TCPServer.__init__(self, server_address, RequestHandlerClass)
|
||||||
loop.close()
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
server = SimpleServer((HOST, PORT), SingleTCPHandler)
|
||||||
|
# terminate with Ctrl-C
|
||||||
|
try:
|
||||||
|
server.serve_forever()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sys.exit(0)
|
Loading…
Reference in New Issue