[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1
input type option values may transmit hidden data that embeds state information
in pages (type=hidden), reinitializes fields (type=reset), or makes multiple-choice
buttons (type=checkbox).

Submission method: get and post
Forms also include a method option to specify the encoding style to be used to send
data over a socket to the target server machine. Here, we use the post style, which
contacts the server and then ships it a stream of user input data in a separate trans-
mission over the socket.
An alternative get style ships input information to the server in a single transmis-
sion step by appending user inputs to the query string at the end of the URL used
to invoke the script, usually after a? character. Query parameters were introduced
earlier when we met URLs; we will put them to use later in this section.
With get, inputs typically show up on the server in environment variables or as
arguments in the command line used to start the script. With post, they must be
read from standard input and decoded. Because the get method appends inputs to
URLs, it allows users to bookmark actions with parameters for later submission
(e.g., a link to a retail site, together with the name of a particular item); post is very
generally meant for sending data that is to be submitted once (e.g., comment text).
The get method is usually considered more efficient, but it may be subject to length
limits in the operating system and is less secure (parameters may be recorded in
server logs, for instance). post can handle larger inputs and may be more secure in
some scenarios, but it requires an extra transmission. Luckily, Python’s cgi module
transparently handles either encoding style, so our CGI scripts don’t normally need
to know or care which is used.


Notice that the action URL in this example’s form spells out the full address for illus-
tration. Because the browser remembers where the enclosing HTML page came from,
it works the same with just the script’s filename, as shown in Example 15-7.


Example 15-7. PP4E\Internet\Web\tutor3-minimal.html



CGI 101

A first user interaction: forms





Enter your name:





It may help to remember that URLs embedded in form action tags and hyperlinks are
directions to the browser first, not to the script. The tutor3.py script itself doesn’t care
which URL form is used to trigger it—minimal or complete. In fact, all parts of a URL


Climbing the CGI Learning Curve| 1151
Free download pdf