-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
82 lines (71 loc) · 2.81 KB
/
app.py
File metadata and controls
82 lines (71 loc) · 2.81 KB
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
import os
import platform
from flask import Flask, render_template, request, redirect, url_for, flash
from werkzeug.utils import secure_filename
import subprocess
import signal
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['ALLOWED_EXTENSIONS'] = {'py'}
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB
app.secret_key = 'supersecretkey' # Set a secret key for flash messages
# Ensure the UPLOAD_FOLDER exists
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.mkdir(app.config['UPLOAD_FOLDER'])
script_process = None
def allowed_file(filename):
return filename.lower().endswith('.py')
@app.route('/')
def index():
files = os.listdir(app.config['UPLOAD_FOLDER'])
return render_template('index.html', files=files)
@app.route('/upload', methods=['POST'])
def upload():
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
flash('File uploaded successfully!', 'success')
else:
flash('Invalid file format!', 'error')
return redirect(url_for('index'))
@app.route('/delete/<filename>', methods=['POST'])
def delete(filename):
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
try:
if os.path.exists(filepath):
os.remove(filepath)
flash(f'{filename} deleted successfully!', 'success')
else:
flash(f'{filename} not found!', 'error')
except Exception as e:
flash(f'Error deleting {filename}: {str(e)}', 'error')
return redirect(url_for('index'))
@app.route('/run', methods=['POST'])
def run():
global script_process
try:
if 'filename' in request.form:
filename = request.form['filename']
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
script_process = subprocess.Popen(['python', filepath])
flash('Script started successfully!', 'success')
else:
flash('Filename field missing in the request!', 'error')
except Exception as e:
flash(f'Error starting script: {str(e)}', 'error')
return redirect(url_for('index'))
@app.route('/stop', methods=['POST'])
def stop():
global script_process
try:
if script_process:
script_process.terminate() # Terminate the script process
script_process = None # Set it to None after termination
flash('Script stopped successfully!', 'success')
except Exception as e:
flash(f'Error stopping script: {str(e)}', 'error')
return redirect(url_for('index'))
if __name__ == '__main__':
app.debug = True # Enable debugging mode
app.run(host='0.0.0.0', port=5000)