C:\...\PP4E\Internet\Web\cgi-bin> python formMockup.py
Bob
hacker
Spam eggs ham
38
Brian
Since the mock-up now lives in a module, we can reuse it anytime we want to test a
CGI script offline. To illustrate, the script in Example 15-22 is a rewrite of the
tutor5.py example we saw earlier, using the form mock-up utility to simulate field in-
puts. If we had planned ahead, we could have tested the script like this without even
needing to connect to the Net.
Example 15-22. PP4E\Internet\Web\cgi-bin\tutor5_mockup.py
#!/usr/bin/python
"""
run tutor5 logic with formMockup instead of cgi.FieldStorage()
to test: python tutor5_mockup.py > temp.html, and open temp.html
"""
from formMockup import formMockup
form = formMockup(name='Bob',
shoesize='Small',
language=['Python', 'C++', 'HTML'],
comment='ni, Ni, NI')
rest same as original, less form assignment
Running this script from a simple command line shows us what the HTML response
stream will look like:
C:\...\PP4E\Internet\Web\cgi-bin> python tutor5_mockup.py
Content-type: text/html
<TITLE>tutor5.py</TITLE>
<H1>Greetings</H1>
<HR>
<H4>Your name is Bob</H4>
<H4>You wear rather Small shoes</H4>
<H4>Your current job: (unknown)</H4>
<H4>You program in Python and C++ and HTML</H4>
<H4>You also said:</H4>
<P>ni, Ni, NI</P>
<HR>
Running it live yields the page in Figure 15-26. Field inputs are hardcoded, similar in
spirit to the tutor5 extension that embedded input parameters at the end of hyperlink
URLs. Here, they come from form mock-up objects created in the reply script that
cannot be changed without editing the script. Because Python code runs immediately,
though, modifying a Python script during the debug cycle goes as quickly as you can
type.
1198 | Chapter 15: Server-Side Scripting