54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
from flask import Flask, render_template, request, jsonify, url_for
|
|
import subprocess
|
|
import json
|
|
import logging
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Set up logging
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return render_template('index.html')
|
|
|
|
@app.route('/favicon.ico')
|
|
def favicon():
|
|
return url_for('static', filename='favicon.ico')
|
|
|
|
@app.route('/run_script', methods=['POST'])
|
|
def run_script():
|
|
settings = request.json
|
|
logging.info(f"Received settings: {settings}")
|
|
|
|
# Ensure Count is an integer
|
|
settings['Count'] = int(settings['Count'])
|
|
|
|
with open('config.json', 'w') as json_file:
|
|
json.dump(settings, json_file)
|
|
logging.info("Settings written to config.json")
|
|
|
|
try:
|
|
# Add a timeout to the subprocess to prevent it from hanging indefinitely
|
|
result = subprocess.run(['python3', 'main.py', 'config.json'], capture_output=True, text=True, timeout=15)
|
|
output = result.stdout
|
|
error = result.stderr
|
|
logging.info(f"Script output: {output}")
|
|
logging.error(f"Script error: {error}")
|
|
except subprocess.TimeoutExpired as e:
|
|
error_message = "The script execution timed out."
|
|
logging.error(error_message)
|
|
output = e.stdout if e.stdout else ""
|
|
error = e.stderr if e.stderr else error_message
|
|
return jsonify({"output": output, "error": error})
|
|
except Exception as e:
|
|
logging.error(f"An error occurred: {str(e)}")
|
|
return jsonify({"error": str(e)})
|
|
|
|
return jsonify({"output": output, "error": error})
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5000, debug=False)
|
|
|