@app.route('/restruct', methods=['POST']) def restruct(): dch = request.form['dch'] dch_folder = "restr" + dch count = 1 while os.path.exists(os.path.join('D:\cj', dch_folder)): dch_folder = "restr" + dch + "_" + str(count) count += 1 os.makedirs(os.path.join('D:\cj', dch_folder, 'photos')) os.makedirs(os.path.join('D:\cj', dch_folder, 'kmls')) os.makedirs(os.path.join('D:\cj', dch_folder, 'project')) photos_folder = os.path.join('D:\cj', dch_folder, 'photos') kmls_folder = os.path.join('D:\cj', dch_folder, 'kmls') project_folder = os.path.join('D:\cj', dch_folder, 'project') for file in request.files.getlist('files'): filename = file.filename file.save(os.path.join(photos_folder, filename)) dhs = request.form.getlist('dh') for dh in dhs: src_path = os.path.join(r'D:/911kml/', dh + '.xlsx.kml') dst_path = os.path.join(r'D:/cj', dch_folder, 'kmls/', dh + '.xlsx.kml') shutil.copy(src_path, dst_path) subprocess.Popen(["python", "restruct.py", dch, photos_folder, project_folder, kmls_folder])使用python3.6版本的websocket,在flask框架中将这段代码运行中的输出信息,实时发送给前端
时间: 2023-06-24 11:08:48 浏览: 205
可以使用 Flask-SocketIO 扩展来实现实时发送输出信息给前端,具体实现步骤如下:
1. 安装 Flask-SocketIO 扩展:
```
pip install flask-socketio
```
2. 在 Flask 应用程序中导入 SocketIO:
```python
from flask_socketio import SocketIO, emit
socketio = SocketIO(app)
```
3. 修改 restruct 视图函数,在执行 subprocess.Popen() 之前,发送一个消息给前端:
```python
@socketio.on('connect')
def test_connect():
emit('my_response', {'data': 'Connected'})
@app.route('/restruct', methods=['POST'])
def restruct():
dch = request.form['dch']
dch_folder = "restr" + dch
count = 1
while os.path.exists(os.path.join('D:\cj', dch_folder)):
dch_folder = "restr" + dch + "_" + str(count)
count += 1
os.makedirs(os.path.join('D:\cj', dch_folder, 'photos'))
os.makedirs(os.path.join('D:\cj', dch_folder, 'kmls'))
os.makedirs(os.path.join('D:\cj', dch_folder, 'project'))
photos_folder = os.path.join('D:\cj', dch_folder, 'photos')
kmls_folder = os.path.join('D:\cj', dch_folder, 'kmls')
project_folder = os.path.join('D:\cj', dch_folder, 'project')
for file in request.files.getlist('files'):
filename = file.filename
file.save(os.path.join(photos_folder, filename))
dhs = request.form.getlist('dh')
for dh in dhs:
src_path = os.path.join(r'D:/911kml/', dh + '.xlsx.kml')
dst_path = os.path.join(r'D:/cj', dch_folder, 'kmls/', dh + '.xlsx.kml')
shutil.copy(src_path, dst_path)
socketio.emit('my_response', {'data': 'Restructuring started'})
subprocess.Popen(["python", "restruct.py", dch, photos_folder, project_folder, kmls_folder])
return 'success'
```
4. 在前端页面中使用 SocketIO 客户端连接 Flask 服务器,并监听 my_response 事件,接收输出信息:
```html
<!DOCTYPE html>
<html>
<head>
<title>WebSocket Test</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.1/socket.io.js"></script>
<script type="text/javascript" charset="utf-8">
var socket = io.connect('http://' + document.domain + ':' + location.port);
socket.on('my_response', function(msg) {
console.log(msg.data);
});
</script>
</head>
<body>
<h1>WebSocket Test</h1>
</body>
</html>
```
当视图函数执行到 subprocess.Popen() 时,会向前端发送一个消息,提示重构开始,然后在 restruct.py 脚本执行过程中,输出信息会实时发送给前端,可以通过在 restruct.py 中使用 print() 或者 sys.stdout.write() 来输出信息。
阅读全文