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

(yzsuai) #1

This static HTML file defines three hyperlinks—the first two are minimal and the third
is fully specified, but all work similarly (again, the target script doesn’t care). When we
visit this file’s URL, we see the page shown in Figure 15-17. It’s mostly just a page for
launching canned calls to the CGI script. (I’ve reduced the text font size here to fit in
this book: run this live if you have trouble reading it here.)


Figure 15-17. Hyperlinks page created by tutor5c.html


Clicking on this page’s second link creates the response page in Figure 15-18. This link
invokes the CGI script, with the name parameter set to “Tom” and the language pa-
rameter set to “Python,” simply because those parameters and values are hardcoded in
the URL listed in the HTML for the second hyperlink. As such, hyperlinks with pa-
rameters like this are sometimes known as stateful links—they automatically direct the
next script’s operation. The net effect is exactly as if we had manually typed the line
shown at the top of the browser in Figure 15-18.


Notice that many fields are missing here; the tutor5.py script is smart enough to detect
and handle missing fields and generate an unknown message in the reply page. It’s also
worth pointing out that we’re reusing the Python CGI script again. The script itself is
completely independent of both the user interface format of the submission page, as
well as the technique used to invoke it—from a submitted form or a hardcoded URL
with query parameters. By separating such user interface details from processing logic,
CGI scripts become reusable software components, at least within the context of the
CGI environment.


Climbing the CGI Learning Curve| 1171
Free download pdf