diff --git a/app.py b/app.py index d857efe..0a943b7 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,7 @@ import threading import time import subprocess +import json try: # Python 2.x from urlparse import urlparse @@ -88,6 +89,15 @@ def return_code(self): return self._return_code +def _get_request_json(): + if request.is_json: + return request.get_json() + data = request.data + if isinstance(data, bytes): + data = data.decode('utf-8') + return json.loads(data) + + def _execute(command: str): success = False try: @@ -111,7 +121,15 @@ def _execute(command: str): @app.route('/execute', methods=['POST']) def execute(): - req = request.get_json() + try: + req = _get_request_json() + except Exception as err: + app.logger.exception("_get_request_json err") + return jsonify({ + 'response': 'Exception: %s' % str(err), + 'success': False + }) + response, success = _execute(req['command']) return jsonify({ @@ -124,7 +142,15 @@ def execute(): def batch_execute(): all_succeeded = True responses = [] - req = request.get_json() + try: + req = _get_request_json() + except Exception as err: + app.logger.exception("_get_request_json err") + return jsonify({ + 'response': 'Exception: %s' % str(err), + 'success': False + }) + commands = req['commands'] for command in commands: response, success = _execute(command) diff --git a/requirements.txt b/requirements.txt index 071f255..3765fca 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,21 @@ -flask -flask_redis -flask_redis_sentinel -flask_bootstrap +flask==3.0.3 +flask_redis==0.4.0 +flask_redis_sentinel==2.0.1 +flask_bootstrap==3.3.7.1 +async-timeout==4.0.3 +blinker==1.8.1 +click==8.1.7 +dominate==2.9.1 +importlib_metadata==7.1.0 +itsdangerous==2.2.0 +Jinja2==3.1.4 +MarkupSafe==2.1.5 +pip==23.0.1 +redis==5.0.4 +Redis-Sentinel-Url==1.0.1 +setuptools==57.5.0 +six==1.16.0 +visitor==0.1.3 +Werkzeug==3.0.3 +wheel==0.43.0 +zipp==3.18.1