tail -f在webbrowser [英] tail -f in a webbrowser

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

问题描述

我创建了一个Python脚本来监视日志文件的变化(例如tail -f)并在控制台上显示它。我想在webbrowser中访问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在webbrowser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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