From 230bdca0ea290541f6d7f835ff7dc8571fc9f506 Mon Sep 17 00:00:00 2001 From: Alvaro Neira Ayuso Date: Thu, 16 Jan 2020 17:52:23 +0100 Subject: Execute cmd command using thread This patch prepares the code for future stop command. --- src/ogRest.py | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/ogRest.py b/src/ogRest.py index b669be4..3775a56 100644 --- a/src/ogRest.py +++ b/src/ogRest.py @@ -47,8 +47,23 @@ class restResponse(): class ogThread(): # Executing cmd thread - def execcmd(httpparser): - return ogOperations.execCMD(httpparser) + def execcmd(client, httpparser): + if httpparser.getCMD() == None: + client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) + return + + try: + shellout = ogOperations.execCMD(httpparser) + except ValueError as err: + client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) + return + + if httpparser.getEcho(): + jsonResp = jsonResponse() + jsonResp.addElement('out', shellout) + client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) + else: + client.send(restResponse.getResponse(ogResponses.OK)) # Powering off thread def poweroff(): @@ -180,23 +195,7 @@ class ogRest(): client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) def process_shellrun(self, client, httpparser): - if httpparser.getCMD() == None: - client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) - return - - try: - shellout = ogThread.execcmd(httpparser) - except ValueError as err: - print(err.args[0]) - client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) - return - - if httpparser.getEcho(): - jsonResp = jsonResponse() - jsonResp.addElement('out', shellout) - client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) - else: - client.send(restResponse.getResponse(ogResponses.OK)) + threading.Thread(target=ogThread.execcmd, args=(client, httpparser,)).start() def process_session(self, client, httpparser): threading.Thread(target=ogThread.procsession, args=(client, httpparser,)).start() -- cgit v1.2.3-18-g5258