22import threading
33import time
44import subprocess
5+ import json
56try :
67 # Python 2.x
78 from urlparse import urlparse
@@ -88,6 +89,15 @@ def return_code(self):
8889 return self ._return_code
8990
9091
92+ def _get_request_json ():
93+ if request .is_json :
94+ return request .get_json ()
95+ data = request .data
96+ if isinstance (data , bytes ):
97+ data = data .decode ('utf-8' )
98+ return json .loads (data )
99+
100+
91101def _execute (command : str ):
92102 success = False
93103 try :
@@ -111,7 +121,15 @@ def _execute(command: str):
111121
112122@app .route ('/execute' , methods = ['POST' ])
113123def execute ():
114- req = request .get_json ()
124+ try :
125+ req = _get_request_json ()
126+ except Exception as err :
127+ app .logger .exception ("_get_request_json err" )
128+ return jsonify ({
129+ 'response' : 'Exception: %s' % str (err ),
130+ 'success' : False
131+ })
132+
115133 response , success = _execute (req ['command' ])
116134
117135 return jsonify ({
@@ -124,7 +142,15 @@ def execute():
124142def batch_execute ():
125143 all_succeeded = True
126144 responses = []
127- req = request .get_json ()
145+ try :
146+ req = _get_request_json ()
147+ except Exception as err :
148+ app .logger .exception ("_get_request_json err" )
149+ return jsonify ({
150+ 'response' : 'Exception: %s' % str (err ),
151+ 'success' : False
152+ })
153+
128154 commands = req ['commands' ]
129155 for command in commands :
130156 response , success = _execute (command )
0 commit comments