From 1a9d09bcdc0d6b6ee1339931485a6900f5bbdf6e Mon Sep 17 00:00:00 2001 From: Srihari Vishnu Date: Fri, 31 Jul 2020 20:06:29 -0400 Subject: [PATCH 1/7] add killing process --- server.py | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/server.py b/server.py index b838abf..f55d734 100644 --- a/server.py +++ b/server.py @@ -40,6 +40,7 @@ def __init__(self, sock, address): self.receivingFile = False self.fileBuilder = "" self.DELIMETER = " cnysetomer" + self.cProcess = None def send_msg(self, message): self._logger.info("%s S - %s" % (self.address[0][12:], message)) @@ -48,6 +49,7 @@ def send_msg(self, message): def get_msg(self): data = str(self.sock.recv(1024).decode("utf-8")) if len(data) == 0: + self._logger.debug("Data is empty") self.stopped = True self._logger.info("%s R %s" % (self.address[0][12:], data)) return data @@ -60,16 +62,10 @@ def handle_request(self, msg): self.fileBuilder = msg.split(' ', 1)[1] elif self.receivingFile: self.fileBuilder += str(msg) + elif str(msg).find('killlast') != -1: + self.killCurrentProcess() else: - try: - #self.send_msg("::start::") - result = subprocess.check_output(msg, shell=True).decode('utf-8').strip() - if not len(result): - self.send_msg("the command '%s' returns nothing " % msg) - for line in result.splitlines(): - self.send_msg(line + " ") - except: - self.send_msg("Error when trying to run the command '%s' " % msg) + self.sendToCLI(msg) #finally: #self.send_msg("::end::") if self.fileBuilder.find(self.DELIMETER) != -1: @@ -88,10 +84,43 @@ def run(self): if self.stopped: break except Exception as e: + self._logger.error("ERROR: %s" % e) pass self.sock.close() self._logger.info("Disconnected from %s" % self.address[0]) + def killCurrentProcess(self): + if (self.cProcess != None): + self._logger.info("Killing process...") + self.cProcess.kill() + self.cProcess = None + else: + self._logger.info("Process is null...") + + + def sendToCLI(self, message): + with subprocess.Popen(message, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True) as process: + self.cProcess = process + try: + stdout, stderr = process.communicate() + except subprocess.TimeoutExpired: + process.kill() + stdout, stderr = process.communicate() + self.send_msg("Timeout when trying to send %s" % message) + except: # Including KeyboardInterrupt, communicate handled that. + process.kill() + return + retcode = process.poll() + if retcode: + self.send_msg(stdout.decode("utf-8").strip()) + else: + result = stdout.decode("utf-8").strip() + if not len(result): + self.send_msg("the command '%s' returns nothing " % msg) + # for line in result.splitlines(): + # self.send_msg(line + " ") + self.send_msg(result) + class Server(): def __init__(self): self.q_len = 3 From e8a6610ddb38cd0527739315f73e07370d31db0c Mon Sep 17 00:00:00 2001 From: Srihari Vishnu Date: Wed, 5 Aug 2020 21:58:45 -0400 Subject: [PATCH 2/7] add new threading --- server.py | 51 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/server.py b/server.py index f55d734..f16783f 100644 --- a/server.py +++ b/server.py @@ -97,29 +97,38 @@ def killCurrentProcess(self): else: self._logger.info("Process is null...") + def startProcess(self, process): + self._logger.info("Starting Process...") + try: + self._logger.info("Start Communicating...") + stdout, stderr = process.communicate() + self._logger.info("Finish Communicating...") + except subprocess.TimeoutExpired: + process.kill() + stdout, stderr = process.communicate() + self.send_msg("Timeout when trying to send %s" % message) + except Exception as e: # Including KeyboardInterrupt, communicate handled that. + self._logger.error("ERROR...%s" % e) + process.kill() + return + retcode = process.poll() + if retcode: + self._logger.info("Returned...") + self.send_msg(stdout.decode("utf-8").strip()) + else: + self._logger.info("Result...") + result = stdout.decode("utf-8").strip() + if not len(result): + self.send_msg("the command '%s' returns nothing " % msg) + # for line in result.splitlines(): + # self.send_msg(line + " ") + self.send_msg(result) def sendToCLI(self, message): - with subprocess.Popen(message, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True) as process: - self.cProcess = process - try: - stdout, stderr = process.communicate() - except subprocess.TimeoutExpired: - process.kill() - stdout, stderr = process.communicate() - self.send_msg("Timeout when trying to send %s" % message) - except: # Including KeyboardInterrupt, communicate handled that. - process.kill() - return - retcode = process.poll() - if retcode: - self.send_msg(stdout.decode("utf-8").strip()) - else: - result = stdout.decode("utf-8").strip() - if not len(result): - self.send_msg("the command '%s' returns nothing " % msg) - # for line in result.splitlines(): - # self.send_msg(line + " ") - self.send_msg(result) + self.cProcess = subprocess.Popen(message, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True) + thread = threading.Thread(target = self.startProcess, args=(self.cProcess, )) + thread.start() + class Server(): def __init__(self): From 9dd86faa24a0fe37dc9e816310dd6ddbe67a1c24 Mon Sep 17 00:00:00 2001 From: Srihari Vishnu Date: Wed, 5 Aug 2020 23:01:27 -0400 Subject: [PATCH 3/7] use process list --- server.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/server.py b/server.py index f16783f..cf4fd7d 100644 --- a/server.py +++ b/server.py @@ -40,7 +40,7 @@ def __init__(self, sock, address): self.receivingFile = False self.fileBuilder = "" self.DELIMETER = " cnysetomer" - self.cProcess = None + self.processes = [] def send_msg(self, message): self._logger.info("%s S - %s" % (self.address[0][12:], message)) @@ -66,8 +66,6 @@ def handle_request(self, msg): self.killCurrentProcess() else: self.sendToCLI(msg) - #finally: - #self.send_msg("::end::") if self.fileBuilder.find(self.DELIMETER) != -1: now = datetime.datetime.now() copyfile(sys.argv[0], sys.argv[0] + now.strftime("%Y%m%d%H%M")) @@ -89,18 +87,16 @@ def run(self): self.sock.close() self._logger.info("Disconnected from %s" % self.address[0]) - def killCurrentProcess(self): - if (self.cProcess != None): - self._logger.info("Killing process...") - self.cProcess.kill() - self.cProcess = None + def killLastProcess(self): + if (self.processes): + self._logger.info("Killing last process...") + self.processes.pop().kill() else: - self._logger.info("Process is null...") + self._logger.info("Processes is empty...") def startProcess(self, process): self._logger.info("Starting Process...") try: - self._logger.info("Start Communicating...") stdout, stderr = process.communicate() self._logger.info("Finish Communicating...") except subprocess.TimeoutExpired: @@ -112,21 +108,23 @@ def startProcess(self, process): process.kill() return retcode = process.poll() - if retcode: + if retcode: # ERROR OCCURRED self._logger.info("Returned...") - self.send_msg(stdout.decode("utf-8").strip()) + self.send_msg("ERROR: %s" % stdout.decode("utf-8").strip()) else: self._logger.info("Result...") result = stdout.decode("utf-8").strip() if not len(result): self.send_msg("the command '%s' returns nothing " % msg) - # for line in result.splitlines(): - # self.send_msg(line + " ") self.send_msg(result) + + if (process in self.processes): + self.processes.remove(process) def sendToCLI(self, message): - self.cProcess = subprocess.Popen(message, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True) - thread = threading.Thread(target = self.startProcess, args=(self.cProcess, )) + process = subprocess.Popen(message, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True) + self.processes.append(process) + thread = threading.Thread(target = self.startProcess, args=(process, )) thread.start() From c98f01dee4cd408ca7233f6730521cf4e9ebed86 Mon Sep 17 00:00:00 2001 From: Srihari Vishnu Date: Wed, 5 Aug 2020 23:15:41 -0400 Subject: [PATCH 4/7] do killall processes --- server.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/server.py b/server.py index cf4fd7d..c311f5b 100644 --- a/server.py +++ b/server.py @@ -62,8 +62,8 @@ def handle_request(self, msg): self.fileBuilder = msg.split(' ', 1)[1] elif self.receivingFile: self.fileBuilder += str(msg) - elif str(msg).find('killlast') != -1: - self.killCurrentProcess() + elif str(msg).find('killall') != -1: + self.killAllProcesses() else: self.sendToCLI(msg) if self.fileBuilder.find(self.DELIMETER) != -1: @@ -87,18 +87,18 @@ def run(self): self.sock.close() self._logger.info("Disconnected from %s" % self.address[0]) - def killLastProcess(self): - if (self.processes): - self._logger.info("Killing last process...") - self.processes.pop().kill() - else: - self._logger.info("Processes is empty...") + def killAllProcesses(self): + self._logger.info("Killing %d processes..." % len(self.processes)) + for process in self.processes: + process.kill() + self.processes = [] + self._logger.info("Killed all processes") def startProcess(self, process): self._logger.info("Starting Process...") try: stdout, stderr = process.communicate() - self._logger.info("Finish Communicating...") + self._logger.info("Finish Communicating") except subprocess.TimeoutExpired: process.kill() stdout, stderr = process.communicate() @@ -109,10 +109,10 @@ def startProcess(self, process): return retcode = process.poll() if retcode: # ERROR OCCURRED - self._logger.info("Returned...") - self.send_msg("ERROR: %s" % stdout.decode("utf-8").strip()) + self._logger.info("Returned Error %s" % stderr.decode("utf-8").strip()) + self.send_msg("ERROR: %s %s" % (stdout.decode("utf-8").strip(), stderr.decode("utf-8").strip())) else: - self._logger.info("Result...") + self._logger.info("Result") result = stdout.decode("utf-8").strip() if not len(result): self.send_msg("the command '%s' returns nothing " % msg) From d4b66bd5791dccfa093eaad5b5702a3934acaa14 Mon Sep 17 00:00:00 2001 From: Srihari Vishnu Date: Thu, 6 Aug 2020 00:01:19 -0400 Subject: [PATCH 5/7] show interrupted command --- server.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/server.py b/server.py index c311f5b..4d33800 100644 --- a/server.py +++ b/server.py @@ -94,7 +94,7 @@ def killAllProcesses(self): self.processes = [] self._logger.info("Killed all processes") - def startProcess(self, process): + def startProcess(self, process, message): self._logger.info("Starting Process...") try: stdout, stderr = process.communicate() @@ -109,8 +109,10 @@ def startProcess(self, process): return retcode = process.poll() if retcode: # ERROR OCCURRED - self._logger.info("Returned Error %s" % stderr.decode("utf-8").strip()) - self.send_msg("ERROR: %s %s" % (stdout.decode("utf-8").strip(), stderr.decode("utf-8").strip())) + if (process in self.processes): + self.send_msg("ERROR: %s %s" % (stdout.decode("utf-8").strip(), stderr.decode("utf-8").strip())) + else: + self._logger.info("Interrupted %s" % message) else: self._logger.info("Result") result = stdout.decode("utf-8").strip() @@ -124,7 +126,7 @@ def startProcess(self, process): def sendToCLI(self, message): process = subprocess.Popen(message, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True) self.processes.append(process) - thread = threading.Thread(target = self.startProcess, args=(process, )) + thread = threading.Thread(target = self.startProcess, args=(process, message, )) thread.start() From 44078a250e81a4940a92a4ce244161be0b0a6dbb Mon Sep 17 00:00:00 2001 From: Srihari Vishnu Date: Thu, 6 Aug 2020 00:20:49 -0400 Subject: [PATCH 6/7] remove strips --- server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server.py b/server.py index 236030a..88e5b28 100644 --- a/server.py +++ b/server.py @@ -110,12 +110,12 @@ def startProcess(self, process, message): retcode = process.poll() if retcode: # ERROR OCCURRED if (process in self.processes): - self.send_msg("ERROR: %s %s" % (stdout.decode("utf-8").strip(), stderr.decode("utf-8").strip())) + self.send_msg("ERROR: %s %s" % (stdout.decode("utf-8"), stderr.decode("utf-8"))) else: self._logger.info("Interrupted %s" % message) else: self._logger.info("Result") - result = stdout.decode("utf-8").strip() + result = stdout.decode("utf-8") if not len(result): self.send_msg("the command '%s' returns nothing " % msg) self.send_msg(result) From e3f5d7d8c2ba56a579ade762b39a9710a95dfdfb Mon Sep 17 00:00:00 2001 From: Srihari Vishnu Date: Thu, 6 Aug 2020 00:22:20 -0400 Subject: [PATCH 7/7] send only if command has content --- server.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server.py b/server.py index 88e5b28..2524268 100644 --- a/server.py +++ b/server.py @@ -118,7 +118,8 @@ def startProcess(self, process, message): result = stdout.decode("utf-8") if not len(result): self.send_msg("the command '%s' returns nothing " % msg) - self.send_msg(result) + else: + self.send_msg(result) if (process in self.processes): self.processes.remove(process)