这个问题在这里已有答案:
我想用python flask或bottle模块创建一个脚本,用/var/log/messages
命令打印我的tail -f
日志。
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\ actually block current thread, it never
return`直到你终止它。
https://github.com/seb-m/pyinotify或http://pythonhosted.org/watchdog/将是一个更好的主意。