Python script web command line

These are my first questions here, so I hope this gets done right;)

I was assigned the task of providing a web interface for some kind of "home" python script. This script is used to check the accessibility of some websites / applications through curl commands. A very important aspect of this script is that it gives its results in real time, writing line by line to standard output.

By providing a web interface for this script, the main goal is that the script can be easily used from anywhere, for example, via a smartphone. Thus, the web interface should be fairly simple and work "without plugins."

My problem is that most of the solutions that I thought of or found on the Internet (ajax, django, even a simple entry) seem to need to fully generate the page before sending it to the browser, losing this important β€œreal time” ".

Any idea on how to do this correctly?

Thanks in advance.

+7
python
source share
3 answers

Sketch for solution:

Create an HTML file that contains the layout for your web page, with a dedicated DIV for the output of the script:

<html> <body> <div id="scriptoutput"></div> <script type="text/javascript" src="localhost:8000/runscript"/> </body> </html> 

This HTML file can be used from any server you want.

Now write a simple http server that runs the script and converts each line into a javascript command (example in python):

 import os f = os.popen('ping 127.0.0.1') for i in range(30): ln = f.readline() print "document.getElementById('scriptoutput').innerHTML += '<pre>%s</pre><br/>';" % ln 

You can use any CGI / WSGI server for the task, or if performance is not critical, even use Python's own BaseHTTPServer class.

This can do the trick, as most browsers analyze and execute Javascript scripts as they are received (and not only after the request is completed) - note that polling or server memory is not required!

+3
source share

Your task sounds interesting. :-) A scenario that just occurred to you: you constantly clean resources using your home-brew scripts and push the results into your permanent database and caching system - for example, Redis - at the same time. Your caching system / layer serves as the main data source for servicing client requests. Redis fe is a high-performance keystore capable of handling 100K connections per second. Although only the last n (for example, 50 thousand records) matter, the caching system will only keep these records if you concentrate on developing server-side APIs (processing connections, processing requests, reading from Redis) and the interface . Communication between the interface and the backend API can be controlled by WebSocket connections. A fairly new part of the HTML5 specification. Although, however, it is already supported by many versions of browsers released these days. Alternatively, you can opt out of some asynchronous Socket flash files. Websockets basically allow you to maintain persistent connections between the client and server; you can register an event listener that is called for each incoming data file / packet - without endless polling or other material.

0
source share

I hope that I understand your need correctly.

Ajax's idea is to refresh the contents of a page without reloading the entire page. I think this should fit your needs. You may need to change your commands if you want to transfer them. You may need to keep their print magazines on the fly.

Here are some ideas:

  • Write a very simple page with the ability to execute commands (menus, forms ...)

  • When a user requests a command to execute, send an ajax request to the server that is executing the command.

  • Your commands need to be changed to redirect sys.stdout to what stores print logs in the database. You can do this by assigning a sys.stdout object with a write function.

     class MyDbLogger: def __init__(self, ...): """Some initialization""" ... def write(self, s): """write into the database""" ... dbout = MyDbLogger(...) sys.stdout = dbout 
  • The client will try the server again to get the content in the database, and then write it to the page.

  • Comet is certainly a research technology to have real-time behavior. This will avoid regular polling of the server. This may be an improvement to # 4, but it may be a little more difficult to implement.

I hope this helps

0
source share

All Articles