Tail -f in web browser

I created a Python script that monitors the log file for changes (e.g. tail -f) and displays it on the console. I would like to access Python script output in a web browser. What do I need to create this? I was thinking about using Django and jQuery. Any tips or examples are appreciated.

+7
javascript jquery python django console
source share
3 answers

First create a python script that keeps track of the log file for changes. If you only need this for testing debugging purposes, then this is excessive use of Django or other web infrastructure. It is very simple to implement the functionality of the Http web server using sockets. Whenever an Http GET request is made, only make the difference with another request. To achieve this, you need to store in memory the status of each incoming request (for example, the number of the last line in the file).

The jQuery part is actually quite simple. Set the timer using the 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 errors and timeouts to report a problem with the server.

+8
source share

I have no experience with Python or Django, but I assume that you can make a system call like tail in Python and relay the details.

From there I use the jQuery .ajax() call with the javascript setInterval() loop in your Python script and output the results in a div on the web page. In general, a fairly simple solution.

In this case, you really do not need to use the open tail -f system call, because the nature of the JS setInterval() method, the Python script will be called again and again until JS clearInterval() . You summarize your script data in Python or JS depending on where you want to do this work. I would suggest Python, since you would have more robust functions at your fingertips, and you would send less data through an AJAX call. Theoretically, it is likely that there should not be too much logic in the front-panel jQuery code. Just show the data.

+2
source share

Why donโ€™t you output the data to an HTML file? You can run the cron job to run a script that, in turn, would pull out an HTML file that can be accessed from the browser.

0
source share

All Articles