[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

not just a single page, and things like memory leaks become much more costly. For
more on persistent CGI models, and support in Python for things such as FastCGI,
search the Web or consult web-specific resources.


Combining Techniques


Naturally, these techniques may be combined to achieve a variety of memory strategies,
both for interaction sessions and for more permanent storage needs. For example:



  • A web application may use cookies to store a per-user or per-session key on the
    client, and later use that key to index into a server-side database to retrieve the
    user’s or session’s full state information.

  • Even for short-lived session information, URL query parameters or hidden form
    fields may similarly be used to pass a key identifying the session from page to page,
    to be used by the next script to index a server-side database.

  • Moreover, URL query parameters and hidden fields may be generated for tempo-
    rary state memory that spans pages, even though cookies and databases are used
    for retention that must span sessions.


The choice of technique is driven by the application’s storage needs. Although not as
straightforward as the in-memory variables and objects of single process GUI programs
running on a client, with creativity, CGI script state retention is entirely possible.


The Hello World Selector


Let’s get back to writing some code again. It’s time for something a bit more useful than
the examples we’ve seen so far (well, more entertaining, at least). This section presents
a program that displays the basic syntax required by various programming languages
to print the string “Hello World,” the classic language benchmark.


To keep it simple, this example assumes that the string is printed to the standard output
stream in the selected language, not to a GUI or web page. It also gives just the output
command itself, not complete programs. The Python version happens to be a complete
program, but we won’t hold that against its competitors here.


Structurally, the first cut of this example consists of a main page HTML file, along with
a Python-coded CGI script that is invoked by a form in the main HTML page. Because
no state or database data is stored between user clicks, this is still a fairly simple
example. In fact, the main HTML page implemented by Example 15-17 is mostly just
one big pull-down selection list within a form.


Example 15-17. PP4E\Internet\Web\languages.html


Languages

Hello World selector


The Hello World Selector | 1183
Free download pdf