[Tutor] pstree cgi-script

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Jun 16 17:09:07 EDT 2004


> Since Danny's message, I tried his suggestion also:
>
> #!/usr/bin/python
> print "Content-type: text/plain\n\n"
> import os
> cmd = "pstree"
> status = os.system(cmd)
> print status
>
> Oddly. It still does not work.


The environment that CGI's run under is more restricted than your own.
In particular, the PATH is probably a lot more restricted when the CGI
runs under Apache.


Try using an absolute path for your executables.  If pstree lives under
/usr/bin, then:

###
#!/usr/bin/python
print "Content-type: text/plain\n\n"
import os
cmd = "/usr/bin/pstree"
status = os.system(cmd)
print status
###

is better.  When writing any CGI application, all external resources
should probably be absolute-pathed to avoid these kinds of problems.

More importantly, whenever we develop a CGI, we should also use the
invaluable 'cgitb' traceback-reporting module:

    http://www.python.org/doc/lib/module-cgitb.html

If something bad happens during the CGI program execution, the traceback
monitor should capture the error so that you can get some good debugging
output.  It's a lot better than seeing "Internal Server Error".


So try doing:

###
#!/usr/bin/python

import cgitb; cgitb.enable()

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


Good luck!




More information about the Tutor mailing list