@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,将这段代码运行中的输出信息,实时发送给前端
时间: 2023-06-27 20:00:58 浏览: 153
Fingerprint_WBF_USB_DCH_ELAN_Z_V3.5.12201.11001_24387.exe
您可以使用 Flask-SocketIO 扩展来实现实时发送输出信息到前端。首先,您需要安装 Flask-SocketIO 扩展:
```
pip install flask-socketio
```
然后,您可以使用以下代码来实现实时发送输出信息到前端:
```python
from flask_socketio import emit
from subprocess import Popen, PIPE
from threading import Thread
@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)
# 使用 Popen 执行命令,并将输出信息实时发送给前端
process = Popen(["python", "restruct.py", dch, photos_folder, project_folder, kmls_folder], stdout=PIPE, stderr=PIPE)
def send_output():
while True:
output = process.stdout.readline().decode().strip()
if output:
emit('output', {'data': output})
else:
break
thread = Thread(target=send_output)
thread.start()
return 'success'
```
在上面的代码中,我们使用 `Popen` 执行命令,并将其的标准输出(stdout)实时发送给前端。为了避免阻塞 Flask 应用程序的主线程,我们在一个新的线程中执行 `send_output` 函数来实现实时发送输出信息到前端。同时,在前端页面中,您需要使用 SocketIO 来接收输出信息并将其显示在页面上:
```javascript
<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>
<script type="text/javascript">
var socket = io.connect('http://' + document.domain + ':' + location.port);
socket.on('connect', function() {
console.log('Socket connected');
});
socket.on('output', function(data) {
console.log(data);
// 将输出信息添加到页面上
$('#output').append('<p>' + data.data + '</p>');
});
</script>
```
在上面的代码中,我们使用 SocketIO 连接 Flask 应用程序,并在接收到实时输出信息时,将其添加到页面上。请注意,在上面的代码中,我们使用了 jQuery 库来操作 DOM 元素。如果您不想使用 jQuery,您可以使用纯 JavaScript 来实现相同的效果。
阅读全文