How to display python script output in real time on website?

I have a Python script that outputs something every second or two, but takes a long time to complete completely. I want to configure the site so that someone can directly call the script, and the output is sent to the screen while the script is running.

I do not want the user to wait for the script to complete, because then all the output is displayed immediately. I also tried this and the connection always fails.

I do not know what this process is called, what terms I am looking for and what I need to use. CGI? Ajax? You need some serious guidance, thanks!

If that matters, I plan to use Nginx as a web server.

+3
source share
5 answers

First of all, your script should print the title:

Connection: Keep-Alive

Because the browser needs to know what it will have to wait.

And your script should output data without buffering. And stackoverflow has already answered this question .

+2
source

The solution is to flush the output buffer at the selection points in the script execution - I only ever did it in PHP via flush (), but it looks like the Python equivalent :

cgiprint() , sys.stdout.flush(). . , 8 , . Python . : -u, #!/usr/bin/python -u shebang.

+1

nginx CGI, fastcgi wsgi

- webpy nginx

0

http://tools.cherrypy.org/wiki/Comet " " -, HTTP- - , ". , - , Javascript mojo AJAX.

, Comet CherryPy jQuery. - ping . ping , , . , , -, CherryPy : " LINK

. cherrypy, script.

0

, keep alive return , "print" yield. , python script, -.

nginx - gevent bottle , , nginx python.

0

Source: https://habr.com/ru/post/1721022/


All Articles