URLs can become difficult for scripts that expect many complex parameters, other
programs can automate the construction process.
When CGI scripts are invoked with explicit input parameters this way, it’s not too
difficult to see their similarity to functions, albeit ones that live remotely on the Net.
Passing data to scripts in URLs is similar to keyword arguments in Python functions,
both operationally and syntactically. In fact, some advanced web frameworks such as
Zope make the relationship between URLs and Python function calls even more literal:
URLs become more direct calls to Python functions.
Incidentally, if you clear out the name input field in the form input page (i.e., make it
empty) and press Submit, the user name field becomes empty. More accurately, the
browser may not send this field along with the form data at all, even though it is listed
in the form layout HTML. The CGI script detects such a missing field with the dic-
tionary in expression and produces the page captured in Figure 15-9 in response.
Figure 15-9. An empty name field producing an error page
In general, CGI scripts must check to see whether any inputs are missing, partly because
they might not be typed by a user in the form, but also because there may be no form
at all—input fields might not be tacked onto the end of an explicitly typed or con-
structed get-style URL. For instance, if we type the script’s URL without any parameters
at all—by omitting the text from the? and beyond, and visiting http://localhost/cgi-bin/
tutor3.py with an explicitly entered URL—we get this same error response page. Since
we can invoke any CGI through a form or URL, scripts must anticipate both scenarios.
Testing outside browsers with the module urllib.request
Once we understand how to send inputs to forms as query string parameters at the end
of URLs like this, the Python urllib.request module we met in Chapters 1 and 13
becomes even more useful. Recall that this module allows us to fetch the reply generated
Climbing the CGI Learning Curve| 1155