【问题标题】:How to tail -f log file with python on browser [duplicate]如何在浏览器上使用python尾部-f日志文件[重复]
【发布时间】:2018-03-01 22:20:19
【问题描述】:

我想使用 python flask 或 bottle 模块创建一个脚本,以使用 tail -f 命令打印我的 /var/log/messages 日志。

import subprocess
from bottle import route, response, run

@route("/")
def file():
    response.content_type = "text/plain"
    while True:
        return subprocess.check_output(["tail", "-4", "file"])

run(host='localhost', port=888)

当我尝试使用 tail -f 时,页面会永远挂起并加载。

【问题讨论】:

  • tail -f 实际上阻塞了当前进程。当你在终端上输入它时,它也会阻止你,对吧?
  • 脚本运行良好,但在 localhost:888/ 页面永远加载,没有输出。

标签: python flask subprocess bottle


【解决方案1】:

tail -f\ actually block current thread, it neverreturn` 直到你终止它。

https://github.com/seb-m/pyinotifyhttp://pythonhosted.org/watchdog/ 会更好。

【讨论】:

    猜你喜欢
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多