浏览器中的tail -f [英] tail -f in a webbrowser

查看:19
本文介绍了浏览器中的tail -f的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 Python 脚本,用于监视日志文件的更改(如 tail -f)并将其显示在控制台上.我想在网络浏览器中访问 Python 脚本的输出.我需要什么来创建这个?我正在考虑使用 Django 和 jQuery.非常感谢任何提示或示例.

I've created a Python script that monitors a logfile for changes (like tail -f) and displays it on a console. I would like to access the output of the Python script in a webbrowser. What would I need to create this? I was thinking about using Django and jQuery. Any tips or examples are greatly appreciated.

推荐答案

首先创建一个python脚本来监控日志文件的变化.如果您只需要它来进行调试 - 测试目的,那么使用 Django 或其他 Web 框架就有点过头了.使用套接字实现 Http Web 服务器功能非常容易.每当一个 Http GET 请求到来时,只提供来自不同请求的差异.为了实现这一点,您需要在内存中存储每个请求的状态(例如文件中最后一行的编号).

First create a python script that monitors the log file for changes. If you only need this for debugging - testing purposes, then it is an overkill to use Django or another web framework. It is very easy to implement Http Web server functionality using sockets. Whenever an Http GET request is coming, serve only the difference from the different request. In order to achieve this you need to store in memory the status of every request coming (e.g.number of last line in the file).

jQuery 部分实际上非常简单.使用 setTimeout 函数设置定时器.这样的事情会做:

The jQuery part is actually quite easy. Set up a timer with setTimeout function. Something like this will do:

function doUpdate() {
  $.ajax({type: "GET", url : tailServiceUrl,
          success: function (data) {
             if (data.length > 4)
             {
                // Data are assumed to be in HTML format
                // Return something like <p/> in case of no updates
                $("#logOutputDiv").append(data);
             }
             setTimeout("doUpdate()", 2000);
           }});
}

setTimeout("doUpdate()", 2000);

您还可以为错误和超时创建回调,以报告服务器问题.

You can also create callbacks for error and timeout to report a problem with the server.

这篇关于浏览器中的tail -f的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆