A CGI Framework in Python
A Unified Framework Makes Site Monitoring Easier and Development a Breeze
By A. M. Kuchling
As Web developers create an increasing number of CGI scripts for their sites, they often find themselves repeatedly writing things like error-handling code. Fortunately, it's rather easy to construct a top-level CGI script that acts as a framework for your site and handles errors cleanly. This approach results in less code in the long run, and lets you concentrate on the interesting parts of the job.
In this article, I'll develop a simple CGI framework in Python that can easily be extended and customized to greatly simplify site development. Because of Python's powerful standard library, it doesn't require much code. Finally, I'll develop a simple user-registration scheme that puts the CGI framework into practice.
Design Considerations
In creating the framework, I had several design considerations. First, the user should never see a server error. If a programming error occurs, the framework should trap it and send a detailed error page to the browser, not leave you staring at a "Server error" page in your browser. The error page should reassure users and refer them to your tech-support line or help page.
Another requirement was that the errors be logged to help track down problems. Python provides tracebacks pinpointing the line of code where the error occurred. Tracebacks can be emailed to a maintainer, which eliminates the trial-and-error process of figuring out where the problem lies; I often just keep a mail program running and read the tracebacks as they arrive.