are ideal for storing more complex cross-session information; a shopping cart applica-
tion, for instance, can record items added in the past in a server-side database.
Databases outlive both pages and sessions. Because data is kept explicitly, there is no
need to embed it within the query parameters or hidden form fields of reply pages.
Because the data is kept on the server, there is no need to store it on the client in cookies.
And because such schemes employ general-purpose databases, they are not subject to
the size constraints or optional nature of cookies.
In exchange for their added utility, full-blown databases require more in terms of in-
stallation, administration, and coding. As we’ll see in Chapter 17, luckily the extra
coding part of that trade-off is remarkably simple in Python. Moreover, Python’s da-
tabase interfaces may be used in any application, web-based or otherwise.
Extensions to the CGI Model
Finally, there are more advanced protocols and frameworks for retaining state on the
server, which we won’t cover in this book. For instance, the Zope web application
framework, discussed briefly in Chapter 12, provides a product interface, which allows
for the construction of web-based objects that are automatically persistent.
Other schemes, such as FastCGI, as well as server-specific extensions such as
mod_python for Apache, may attempt to work around the autonomous, one-shot nature
of CGI scripts, or otherwise extend the basic CGI model to support long-lived memory
stores. For example:
- FastCGI allows web applications to run as persistent processes, which receive input
data from and send reply streams to the HTTP web server over Inter-Process Com-
munication (IPC) mechanisms such as sockets. This differs from normal CGI,
which communicates inputs and outputs with environment variables, standard
streams, and command-line arguments, and assumes scripts run to completion on
each request. Because a FastCGI process may outlive a single page, it can retain
state information from page to page, and avoids startup performance costs. - mod_python extends the open source Apache web server by embedding the Python
interpreter within Apache. Python code is executed directly within the Apache
server, eliminating the need to spawn external processes. This package also sup-
ports the concept of sessions, which can be used to store data between pages.
Session data is locked for concurrent access and can be stored in files or in memory,
depending on whether Apache is running in multiprocess or multithreaded mode.
mod_python also includes web development tools, such as the Python Server Pages
(PSP) server-side templating language for HTML generation mentioned in Chap-
ter 12 and earlier in this chapter.
Such models are not universally supported, though, and may come with some added
cost in complexity—for example, to synchronize access to persistent data with locks.
Moreover, a failure in a FastCGI-style web application impacts the entire application,
1182 | Chapter 15: Server-Side Scripting