Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
-
from flask import Flask, render_template
|
2 |
from flask_socketio import SocketIO, emit
|
3 |
from flask_cors import CORS
|
|
|
4 |
import pty
|
5 |
import os
|
6 |
import subprocess
|
@@ -9,6 +10,12 @@ import threading
|
|
9 |
app = Flask(__name__)
|
10 |
CORS(app) # 启用 CORS
|
11 |
app.config['SECRET_KEY'] = 'your_secret_key'
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
socketio = SocketIO(app)
|
13 |
|
14 |
master_fd, slave_fd = pty.openpty()
|
@@ -26,12 +33,36 @@ def run_shell():
|
|
26 |
def index():
|
27 |
return render_template('index.html')
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
@socketio.on('input')
|
30 |
def handle_input(input_data):
|
31 |
os.write(master_fd, (input_data + '\n').encode('utf-8'))
|
32 |
|
33 |
if __name__ == '__main__':
|
|
|
|
|
34 |
shell_thread = threading.Thread(target=run_shell)
|
35 |
shell_thread.daemon = True
|
36 |
shell_thread.start()
|
37 |
-
socketio.run(app, host='0.0.0.0', port=7860, allow_unsafe_werkzeug=True)
|
|
|
1 |
+
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
|
2 |
from flask_socketio import SocketIO, emit
|
3 |
from flask_cors import CORS
|
4 |
+
from flask_uploads import UploadSet, configure_uploads, ALL
|
5 |
import pty
|
6 |
import os
|
7 |
import subprocess
|
|
|
10 |
app = Flask(__name__)
|
11 |
CORS(app) # 启用 CORS
|
12 |
app.config['SECRET_KEY'] = 'your_secret_key'
|
13 |
+
|
14 |
+
# 配置文件上传
|
15 |
+
app.config['UPLOADED_FILES_DEST'] = 'uploads' # 上传文件的保存目录
|
16 |
+
files = UploadSet('files', ALL)
|
17 |
+
configure_uploads(app, files)
|
18 |
+
|
19 |
socketio = SocketIO(app)
|
20 |
|
21 |
master_fd, slave_fd = pty.openpty()
|
|
|
33 |
def index():
|
34 |
return render_template('index.html')
|
35 |
|
36 |
+
@app.route('/upload', methods=['POST'])
|
37 |
+
def upload_file():
|
38 |
+
if 'file' in request.files:
|
39 |
+
filename = files.save(request.files['file'])
|
40 |
+
return redirect(url_for('index'))
|
41 |
+
return 'No file uploaded', 400
|
42 |
+
|
43 |
+
@app.route('/files', defaults={'path': ''})
|
44 |
+
@app.route('/files/<path:path>')
|
45 |
+
def list_files(path):
|
46 |
+
full_path = os.path.join(app.config['UPLOADED_FILES_DEST'], path)
|
47 |
+
if not os.path.exists(full_path):
|
48 |
+
return 'Directory not found', 404
|
49 |
+
|
50 |
+
files_and_dirs = os.listdir(full_path)
|
51 |
+
files_and_dirs = [f for f in files_and_dirs if not f.startswith('.')] # 排除隐藏文件
|
52 |
+
return render_template('files.html', files=files_and_dirs, current_path=path)
|
53 |
+
|
54 |
+
@app.route('/files/<path:path>/<filename>')
|
55 |
+
def download_file(path, filename):
|
56 |
+
return send_from_directory(os.path.join(app.config['UPLOADED_FILES_DEST'], path), filename)
|
57 |
+
|
58 |
@socketio.on('input')
|
59 |
def handle_input(input_data):
|
60 |
os.write(master_fd, (input_data + '\n').encode('utf-8'))
|
61 |
|
62 |
if __name__ == '__main__':
|
63 |
+
if not os.path.exists(app.config['UPLOADED_FILES_DEST']):
|
64 |
+
os.makedirs(app.config['UPLOADED_FILES_DEST'])
|
65 |
shell_thread = threading.Thread(target=run_shell)
|
66 |
shell_thread.daemon = True
|
67 |
shell_thread.start()
|
68 |
+
socketio.run(app, host='0.0.0.0', port=7860, allow_unsafe_werkzeug=True)
|