CGI output

Gerhard =?unknown-8bit?Q?H=E4ring?= gh_pythonlist at
Sat Nov 17 08:49:55 CET 2001

On Fri, Nov 16, 2001 at 06:19:24PM -0800, Sebastien Delafond wrote:
> Hi,
> I'm using python to write a CGI interface providing my users with the
> ability to run a specific command, a.k.a. "command".
> This command takes about 3 to minutes to complete under normal
> conditions, and the current Python solution I'm using to run it
> prevent users to see its output before it's fully completed. I've
> tried calling this command with
> commands.getstatusoutput(command)
> execv(command,[])
> but I still can't manage to do it... How would I call my command so
> that the output is displayed "continuously" to my user on the web page
> ?

If you want to do this, you cannot use commands.getstatusoutput, because this
returns the full output from the command. You could use os.popen and read the
results line by line, then print them. Or just use something like os.system and
start the command as a subprocess. I'd also recommend to start Python with the
option "-u" to avoid any stdout buffering.

This works with my Apache configuration. If it doesn't work as expected, it
might be a webserver configuration problem. For example, the following CGI
script is displayed line by line in my browser:

#import cgi
import sys, time

print 'Content-type: text/plain\n\n'
while 1:
    print '-'

mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    OpenPGP public key id 86AB43C0
public key fingerprint: DEC1 1D02 5743 1159 CD20  A4B6 7B22 6575 86AB 43C0
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))

More information about the Python-list mailing list