我想使用flask,flask-socketio来实现一个在浏览器上接受“命令字符串”(如:ping 8.8.8.8)的应用程序,然后服务器执行它并将命令的输出实时返回给浏览器。
我的实现如下。
客户端向服务器发出 'exec_event' 事件,服务器运行命令并在命令生成新输出时不断向客户端发出 'exec_response' 事件。
如果要终止命令,客户端还可以向服务器发送“ctrlc_event”。
我遇到的问题是:
服务器在处理前一个“exec_event”时无法处理“ctrlc_event”。如果我直接杀死服务器上的“ping 8.8.8.8”等长时间运行的命令,则服务器开始处理“ctrlc_event”。
谁能指出我的原因或给我一些方向?
服务器端代码
from gevent import monkey
monkey.patch_all()
from flask import Flask, render_template, session, Response
from flask.ext.wtf import Form
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
from flask.ext.socketio import SocketIO, emit
import os
import time
from subprocess import Popen, PIPE
class ExecForm(Form):
command = StringField('Command: ', validators=[DataRequired()])
submit = SubmitField('Execute')
class CtrlCForm(Form):
submit = SubmitField('Ctrl C')
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'hard-to-guess'
socketio = SocketIO(app)
@app.route('/')
def index():
exec_form = ExecForm()
ctrlc_form = CtrlCForm()
return render_template('index.html',
exec_form=exec_form,
ctrlc_form=ctrlc_form)
@socketio.on('exec_event', namespace='/test')
def execute(message):
command = message['data']
process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE,
close_fds=True, preexec_fn=os.setsid)
session['pid'] = process.pid
for out in iter(process.stdout.readline, ''):
out = '<pre>' + out + '</pre>'
emit('exec_response', {'data': out})
time.sleep(0.001)
out = "Command finished." + '<br /><br />'
emit('exec_terminate', {'data': out})
@socketio.on('ctrlc_event', namespace='/test')
def ctrlc(message):
print("Received ctrlc")
# do something to kill the process
# finally emit an event.
out = "Process killed." + '<br /><br/>'
emit('ctrlc_response', {'data': out})
if __name__ == '__main__':
socketio.run(app, host='0.0.0.0', port=8080)
客户端代码是
<html>
<head>
<title>Example</title>
<script type="text/javascript" src='//code.jquery.com/jquery-1.11.0.min.js'></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var socket = io.connect('http://' + document.domain + ':' + location.port + '/test');
$('#exec_form').submit(function(event) {
socket.emit('exec_event', {data: $('#exec_input').val()});
return false;
});
$('#ctrlc_form').submit(function(event) {
socket.emit('ctrlc_event', {data: 'nothing'});
return false;
});
socket.on('exec_response', function(msg) {
$('#result').append(msg.data);
});
socket.on('exec_terminate', function(msg) {
$('#result').append(msg.data);
});
socket.on('ctrlc_response', function(msg) {
$('#result').append(msg.data);
});
});
</script>
<style>
.result {
overflow-y: scroll;
width: 100%;
height: 400px;
border: 4px solid red;
}
.result pre {
margin: 2px;
}
</style>
</head>
<body>
<h3>Flask Example</h3>
<form id="exec_form" method='POST' action='#' target='result'>
{{ exec_form.command.label }}
{{ exec_form.command(id="exec_input", placeholder="command", size=100) }}
{{ exec_form.submit(id="exec_btn") }}
</form>
<form id="ctrlc_form" method='POST' action='#' target='result'>
{{ ctrlc_form.submit(id="ctrlc_btn") }}
</form>
<div id="result" class="result"></div>
</body>
</html>