50 lines
1.2 KiB
Python
50 lines
1.2 KiB
Python
import SocketServer
|
|
import threading
|
|
import cmd
|
|
import sys
|
|
|
|
|
|
class MyCmd(cmd.Cmd):
|
|
@classmethod
|
|
def setClassParams(cls, entries, adminPwd):
|
|
cls.entries = entries
|
|
cls.adminPwd = adminPwd
|
|
|
|
def __init__(self, i, o):
|
|
cmd.Cmd.__init__(self, completekey=None, stdin=i, stdout=o)
|
|
cmd.Cmd.use_rawinput = False
|
|
self.intro = "Hello at the yadyn cli\n"
|
|
self.prompt = "yadyn> "
|
|
|
|
def do_hello(self, l):
|
|
self.stdout.write("DO: %s\n" % l)
|
|
|
|
def do_quit(self, l):
|
|
self.stdout.write("Bye\n")
|
|
return True
|
|
|
|
|
|
|
|
class CmdHandler(SocketServer.StreamRequestHandler):
|
|
def handle(self):
|
|
myCmd = MyCmd(self.rfile, self.wfile)
|
|
myCmd.cmdloop()
|
|
|
|
|
|
class CmdServer(SocketServer.ThreadingTCPServer):
|
|
def __init__(self, server_address, RequestHandlerClass):
|
|
self.allow_reuse_address = True
|
|
SocketServer.ThreadingTCPServer.__init__(self, server_address, RequestHandlerClass)
|
|
|
|
|
|
class CmdReceiver(threading.Thread):
|
|
def __init__(self, cmdAddr, entries, adminPwd):
|
|
threading.Thread.__init__(self)
|
|
self.cmdAddr = cmdAddr
|
|
MyCmd.setClassParams(entries, adminPwd)
|
|
self.setDaemon(True)
|
|
|
|
def run(self):
|
|
CmdServer(self.cmdAddr, CmdHandler).serve_forever()
|
|
|