But there’s a catch here: where does the input field’s value come from if there is no form
page? That is, if we type the CGI script’s URL ourselves, how does the input field get
filled in? Earlier, when we talked about URL formats, I mentioned that the get encoding
scheme tacks input parameters onto the end of URLs. When we type script addresses
explicitly, we can also append input values on the end of URLs, where they serve the
same purpose as fields in forms. Moreover, the Python cgi module makes URL
and form inputs look identical to scripts.
For instance, we can skip filling out the input form page completely and directly invoke
our tutor3.py script by visiting a URL of this form (type this in your browser’s address
field):
http://localhost/cgi-bin/tutor3.py?user=Brian
In this URL, a value for the input named user is specified explicitly, as if the user had
filled out the input page. When called this way, the only constraint is that the parameter
name user must match the name expected by the script (and hardcoded in the form’s
HTML). We use just one parameter here, but in general, URL parameters are typically
introduced with a? and are followed by one or more name=value assignments, separated
by & characters if there is more than one. Figure 15-8 shows the response page we get
after typing a URL with explicit inputs.
Figure 15-8. tutor3.py result for parameters in a URL
In fact, HTML forms that specify the get encoding style also cause inputs to be added
to URLs this way. Try changing Example 15-6 to use method=GET, and submit the form—
the name input in the form shows up as a query parameter in the reply page address
field, just like the URL we manually entered in Figure 15-8. Forms can use the post or
get style. Manually typed URLs with parameters use get.
Generally, any CGI script can be invoked either by filling out and submitting a form
page or by passing inputs at the end of a URL. Although hand-coding parameters in
1154 | Chapter 15: Server-Side Scripting