Next, in Example 15-20, we recode the main page as an executable script and populate
the response HTML with values imported from the common module file in the previous
example.
Example 15-20. PP4E\Internet\Web\cgi-bin\languages2.py
#!/usr/bin/python
"""
generate HTML for main page dynamically from an executable Python script,
not a precoded HTML file; this lets us import the expected input field name
and the selection table values from a common Python module file; changes in
either now only have to be made in one place, the Python module file;
"""
REPLY = """Content-type: text/html
Hello World selector
Similar to file languages.html, but
this page is dynamically generated by a Python CGI script, using
selection list and input field names imported from a common Python
module on the server. Only the common module must be maintained as
new languages are added, because it is shared with the reply script.
To see the code that generates this page and the reply, click
here,
here,
here, and
here.
"""
from languages2common import hellos, inputkey
options = []
for lang in hellos: # we could sort keys too
options.append('
Again, ignore the getfile hyperlinks in this file for now; we’ll learn what they mean in
a later section. You should notice, though, that the HTML page definition becomes a
printed Python string here (named REPLY), with %s format targets where we plug in
Refactoring Code for Maintainability | 1195