Saving State Information in CGI Scripts
One of the most unusual aspects of the basic CGI model, and one of its starkest con-
trasts to the GUI programming techniques we studied in the prior part of this book, is
that CGI scripts are stateless—each is a standalone program, normally run autono-
mously, with no knowledge of any other scripts that may run before or after. There is
no notion of things such as global variables or objects that outlive a single step of
interaction and retain context. Each script begins from scratch, with no memory of
where the prior left off.
This makes web servers simple and robust—a buggy CGI script won’t interfere with
the server process. In fact, a flaw in a CGI script generally affects only the single page
it implements, not the entire web-based application. But this is a very different model
from callback-handler functions in a single process GUI, and it requires extra work to
remember things longer than a single script’s execution.
Lack of state retention hasn’t mattered in our simple examples so far, but larger systems
are usually composed of multiple user interaction steps and many scripts, and they
need a way to keep track of information gathered along the way. As suggested in the
Figure 15-20. Response page created by tutor5.py (4)
1174 | Chapter 15: Server-Side Scripting