Kakadvn commited on
Commit
7259e88
·
verified ·
1 Parent(s): 225b90f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, render_template_string, Response
2
+ import subprocess
3
+ from ansi2html import Ansi2HTMLConverter
4
+
5
+ app = Flask(__name__)
6
+ conv = Ansi2HTMLConverter(inline=True)
7
+
8
+ @app.route('/', defaults={'command': ''})
9
+ @app.route('/<path:command>')
10
+ def execute_command(command):
11
+ try:
12
+ if not command:
13
+ return render_template_string('''
14
+ <h1>There's nothing in here:)</h1>
15
+ <form method="GET">
16
+ <input type="text" name="command" placeholder="Enter command">
17
+ <button type="submit">Execute</button>
18
+ </form>
19
+ ''')
20
+
21
+ # Replace underscores with spaces
22
+ command = command.replace("_", " ")
23
+
24
+ # Execute command
25
+ process = subprocess.Popen(
26
+ command,
27
+ shell=True,
28
+ stdout=subprocess.PIPE,
29
+ stderr=subprocess.PIPE,
30
+ text=True
31
+ )
32
+ stdout, stderr = process.communicate()
33
+
34
+ # Convert ANSI codes to HTML
35
+ html_output = conv.convert(stdout + stderr, full=False)
36
+
37
+ return render_template_string('''{{ output | safe }}
38
+ ''', output=html_output)
39
+
40
+ except Exception as e:
41
+ return f"Error: {e}"
42
+
43
+ if __name__ == '__main__':
44
+ app.run(host='0.0.0.0', port=7860)