Piping output to a web browser...

Niklas Frykholm r2d2 at mao.acc.umu.se
Thu Jun 15 03:13:47 EDT 2000


>A while back I saw a thread about keeping a web coonection alive (too bad I
>saw it 4 hours AFTER I came up with the same general plan).  But I am still
>struggling with how I can pipe the output to the screen on a streaming
>basis.  The code I have seems to cache it all up and pushes it to the screen
>all at once once I return out of the function.  Any ideas?
>
>    print "Content-type: text/html"
>    print
>    sys.stdout.flush()

Flushing stdout may not help in a CGI program, because your output is not
sent directly to the user, it is sent to the web server and from there
it is relayed to the user. The web server keeps its own buffers and your
output might get stuck there.

What you need to do is tell the web server to use unbuffered output,
or that you want to send your output directly to the user. How you do
this differs from web server to web server.

In Apache you specify that you want a direct communication line with
the user by making sure that your script name starts with "nph-" for
non-parsed headers (it is a strange solution, I know). You must also
change your program so that it writes a complete HTTP header starting
with

HTTP/1.1 200 OK

(this is normally added by the server, since you bypass the server
you have to write it yourself)

// Niklas



More information about the Python-list mailing list