-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
83 lines (69 loc) · 2.47 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from flask import Flask, jsonify, request
import subprocess
import json
import re
import logging
import os
app = Flask(__name__)
# Disable Flask's default logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.CRITICAL)
# Function to delete logs.log file if it exists
def delete_log_file():
log_file = 'logs.log'
if os.path.exists(log_file):
os.remove(log_file)
@app.route('/run-a', methods=['POST'])
def run_a():
json_data = request.get_json()
if not json_data:
return jsonify({"error": "No data provided"}), 400
try:
# Convert JSON data to string
json_data_str = json.dumps(json_data)
# Run a.py and capture the output
result = subprocess.run(
['python3', 'a.py'],
input=json_data_str,
capture_output=True, text=True, check=True
)
output = result.stdout.strip()
error_output = result.stderr.strip()
# Extract JSON part from the output using regex
match = re.search(r'{.*}', output, re.DOTALL)
if match:
json_output = match.group(0)
delete_log_file()
return jsonify(json.loads(json_output)), 200
else:
delete_log_file()
return jsonify({"result": output, "stderr": error_output}), 200
except subprocess.CalledProcessError as e:
return jsonify({"error": str(e), "output": e.output, "stderr": e.stderr}), 500
@app.route('/run-b', methods=['POST'])
def run_b():
json_data = request.get_json()
if not json_data:
return jsonify({"error": "No data provided"}), 400
try:
# Convert JSON data to string
json_data_str = json.dumps(json_data)
# Run a.py and capture the output
result = subprocess.run(
['python3', 'b.py'],
input=json_data_str,
capture_output=True, text=True, check=True
)
output = result.stdout.strip()
error_output = result.stderr.strip()
# Extract JSON part from the output using regex
match = re.search(r'{.*}', output, re.DOTALL)
if match:
json_output = match.group(0)
return jsonify(json.loads(json_output)), 200
else:
return jsonify({"result": output, "stderr": error_output}), 200
except subprocess.CalledProcessError as e:
return jsonify({"error": str(e), "output": e.output, "stderr": e.stderr}), 500
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)