【问题标题】:getting realtime output of the 'top' linux command in a web browser with flask python使用flask python在网络浏览器中获取'top' linux命令的实时输出
【发布时间】:2021-10-06 01:18:59
【问题描述】:

目前,我正在构建一个能够执行 shell 命令并将结果获取到 Web 浏览器的应用程序。 我的问题是结果在浏览器上显示很好,但不是实时的。我遇到问题的命令是 Linux 'top' 命令。正如我们所知,此命令会实时发送系统上执行的进程列表以及 CPU、RAM 和实时。可以用 Flask 做到这一点吗??? 我以前看过类似的帖子,但它们对我没有帮助。坦率地说,我对烧瓶还是很陌生,你的帮助对我来说非常有用。提前致谢

【问题讨论】:

  • 这完全有可能,但您需要使用 Javascript 来实现“实时”方面。

标签: python linux flask command-line eventlet


【解决方案1】:

以下是我尝试过的代码。 (命令在test.sh文件中)

import flask
from shelljob import proc

app = flask.Flask(__name__)

@app.route( '/stream' )
def stream():
    g = proc.Group()
    p = g.run( [ "./test.sh" ] )

    def read_process():
        while g.is_pending():
            
            lines = g.readlines()
            for proc, line in lines:
                yield line
                

    retour = flask.Response( read_process(), mimetype= 'text/json' )
        return retour

@app.route('/exec')
def streaming():
         return flask.render_template('index.html')



if __name__ == "__main__":
    app.run(debug=True)

【讨论】:

    猜你喜欢
    • 2019-02-19
    • 2012-02-07
    • 2015-12-26
    • 1970-01-01
    • 2022-06-15
    • 2014-10-30
    • 2012-07-28
    • 2014-08-10
    • 1970-01-01
    相关资源
    最近更新 更多