-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter.py
More file actions
executable file
·26 lines (22 loc) · 826 Bytes
/
interpreter.py
File metadata and controls
executable file
·26 lines (22 loc) · 826 Bytes
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
from flask import Flask, flash
import subprocess
import os
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'static/uploads'
def interpreter(filename):
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
ext = filename[filename.rfind('.'):].lower()
if ext in ('.php', '.py', '.sh'):
try:
os.chmod(filepath, 0o755)
cmd = {
'.php': ['php', filepath],
'.py': ['python3', filepath],
'.sh': ['bash', filepath]
}[ext]
result = subprocess.run(cmd, capture_output=True, text=True)
return f"<pre>OUTPUT:\n{result.stdout}\nERRORS:\n{result.stderr}</pre>"
except Exception as e:
return f"Execution failed: {str(e)}", 500
else:
return None