door to automated regression testing of CGI scripts—we can invoke scripts on any
remote machine, and compare their reply text to the expected output.§ We’ll see url
lib.request in action again in later examples.
Before we move on, here are a few advanced urllib.request usage notes. First, this
module also supports proxies, alternative transmission modes, the client side of secure
HTTPS, cookies, redirections, and more. For instance, proxies are supported trans-
parently with environment variables or system settings, or by using ProxyHandler
objects in this module (see its documentation for details and examples).
Moreover, although it normally doesn’t make a difference to Python scripts, it is pos-
sible to send parameters in both the get and the put submission modes described earlier
with urllib.request. The get mode, with parameters in the query string at the end of
a URL as shown in the prior listing, is used by default. To invoke post, pass parameters
in as a separate argument:
>>> from urllib.request import urlopen
>>> from urllib.parse import urlencode
>>> params = urlencode({'user': 'Brian'})
>>> params
'user=Brian'
>>>
>>> print(urlopen('http://localhost/cgi-bin/tutor3.py', params).read().decode())
<TITLE>tutor3.py</TITLE>
<H1>Greetings</H1>
<HR>
<P>Hello, Brian.</P>
<HR>
Finally, if your web application depends on client-side cookies (discussed later) these
are supported by urllib.request automatically, using Python’s standard library cookie
support to store cookies locally, and later return them to the server. It also supports
redirection, authentication, and more; the client side of secure HTTP transmissions
(HTTPS) is supported if your computer has secure sockets support available (most do).
See the Python library manual for details. We’ll explore both cookies later in this chap-
ter, and introduce secure HTTPS in the next.
Using Tables to Lay Out Forms
Now let’s move on to something a bit more realistic. In most CGI applications, input
pages are composed of multiple fields. When there is more than one, input labels and
fields are typically laid out in a table, to give the form a well-structured appearance.
The HTML file in Example 15-9 defines a form with two input fields.
§If your job description includes extensive testing of server-side scripts, you may also want to explore Twill,
a Python-based system that provides a little language for scripting the client-side interface to web applications.
Search the Web for details.
Climbing the CGI Learning Curve| 1157