[Tutor] cgi with system calls

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Dec 14 19:57:02 CET 2004



On Tue, 14 Dec 2004, Nik wrote:

> ok, problem solved (well, partly - at least as much as I need it to be).
>
> status = os.system('ps') doesn't set status equal to the output text, it
> sets it to the return of the call (in this case '0'). What I really want
> to do is
>
> status = os.popen('ps').read()
> print status
>
> which works fine. However, why the first version confused the web server
> I don't know, I guess the output from ps went somewhere it wasn't
> supposed to.


Hi Nik,


Yes, the output from 'ps' went to standard output.

In fact, it turns out that Python does some internal buffering on its
output.  The external commands, though, flush their own output when
they're done.  That's at the heart of the problem you were seeing.


With the program:

###
print "Content-type: text/plain\n\n"
status = os.system(cmd)
###


The output from the os.system() came out first, and then the content-type
header.  So your web server got to see something like:

###
PID TTY          TIME CMD
3649 pts0     00:00:00 su
3652 pts0     00:00:00 bash
5197 pts0     00:00:00 ps
Content-type: text/plain

###


Hope this helps!



More information about the Tutor mailing list