flask-socketio中使用cmd,subprocess.Popen实时输出

flask-socketio中使用cmd

#python 3.5
try:
    cmd = 'ping www.baidu.com'
    p = subprocess.Popen(cmd, stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
    while True:
        for line in iter(p.stdout.readline, b''):
            a = line.decode('gbk')
            gevent.sleep(0)  # 关键
            emit('server_default_response', {'data': a})
        if p.poll() is not None:
            break
except Exception as e:
    return False

你可能感兴趣的:(python)