从Python脚本中打印控制台输出

时间:2018-04-19 17:55:37

标签: python flask stream console subprocess

我有一个永远运行的Python脚本,每隔10秒aprox不断地向控制台打印输出。它每10秒钟不会从此控制台消息中返回任何其他内容。

我希望能够捕获此脚本控制台的输出并通过烧录器传输它。

这是我的代码:

@app.route('/stream', methods=["GET"])
def stream():
    g = proc.Group()
    p = g.run(["sudo", "python", "-u", "/home/script_always_running.py"])

    def read_process():
        while g.is_pending():
            lines = g.readlines()
            for proc, line in lines:
                yield "data:" + line + "\n\n"
    return Response(read_process(), mimetype= 'text/event-stream')


@app.route('/stream.html')
def get_page():
    return send_file('stream.html')

由于我不知道的原因,当我在服务器中打开stream.html时,它没有显示任何内容。

这是stream.html的代码

<!DOCTYPE html>
<html>
<head>
    <script>
    var source = new EventSource("/stream");
    source.onmessage = function(event) {
        document.getElementById("output").innerHTML += event.data + "<br/>"
    }
    </script>
</head>
<body>
    <h1>Output</h1>
    <div id="output"></div>
</body>
</html>

你能帮帮我吗?

由于

0 个答案:

没有答案