"Wrap" a python-cgi

Ben Hutchings ben.hutchings at roundpoint.com
Thu Mar 8 23:47:18 EST 2001


Hermann Himmelbauer <dusty at strike.wu-wien.ac.at> writes:

> Hi,
> I am currently using python for several cgi programs. One problem is that 
> if my cgi-script has a bug it exits with an exception and the user gets an 
> ugly error message done by the webserver.
> 
> I would like to put some really simple "bugfree" (if something like this 
> can exist :-) "wrapper" around my cgi-scripts, let it trap Exceptions, log 
> the error and let it output a nice error page to the user.
> 
> My first approach would perhaps be to wrap the whole script into a "try: 
> except" clause but at first I don't know if nested try/except clauses are 
> allowed

They are.

> and moreover the whole script looks somehow ugly as every line is 
> indented.
> 
> Perhaps you know a better solution?

Make the wrapper a script that imports the original script as if it's
a module, inside a try block.  This also means that Python will use a
byte-compiled version of the original script if there is one, saving a
little time.

(But it shouldn't be able to create byte-compiled versions when
running the script through CGI, if you're being sufficiently paranoid
about permissions and ownership of files and processes.)

-- 
Any opinions expressed are my own and not necessarily those of Roundpoint.



More information about the Python-list mailing list