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

(yzsuai) #1
socket. On the client, the browser reads the HTML and uses it to construct the
next page you see.
But if the URL requested by the browser names an executable program instead (e.g.,
a URL ending in .cgi or .py), the HTTP server starts the named program on the
server machine to process the request and redirects the incoming browser data to
the spawned program’s stdin input stream, environment variables, and command-
line arguments. That program started by the server is usually a CGI script—a pro-
gram run on the remote server machine somewhere in cyberspace, usually not on
your computer. The CGI script is responsible for handling the request from this
point on; it may store your information in a database, perform a search, charge
your credit card, and so on.

Response
Ultimately, the CGI script prints HTML, along with a few header lines, to generate
a new response page in your browser. When a CGI script is started, the HTTP
server takes care to connect the script’s stdout standard output stream to a socket
that the browser is listening to. As a result, HTML code printed by the CGI script
is sent over the Internet, back to your browser, to produce a new page. The HTML
printed back by the CGI script works just as if it had been stored and read from an
HTML file; it can define a simple response page or a brand-new form coded to
collect additional information. Because it is generated by a script, it may include
information dynamically determined per request.


In other words, CGI scripts are something like callback handlers for requests generated
by web browsers that require a program to be run dynamically. They are automatically
run on the server machine in response to actions in a browser. Although CGI scripts
ultimately receive and send standard structured messages over sockets, CGI is more
like a higher-level procedural convention for sending and receiving information be-
tween a browser and a server.


Writing CGI Scripts in Python


If all of this sounds complicated, relax—Python, as well as the resident HTTP server,
automates most of the tricky bits. CGI scripts are written as fairly autonomous pro-
grams, and they assume that startup tasks have already been accomplished. The HTTP
web server program, not the CGI script, implements the server side of the HTTP pro-
tocol itself. Moreover, Python’s library modules automatically dissect information sent
up from the browser and give it to the CGI script in an easily digested form. The upshot
is that CGI scripts may focus on application details like processing input data and
producing a result page.


As mentioned earlier, in the context of CGI scripts, the stdin and stdout streams are
automatically tied to sockets connected to the browser. In addition, the HTTP server
passes some browser information to the CGI script in the form of shell environment
variables, and possibly command-line arguments. To CGI programmers, that means:


1128 | Chapter 15: Server-Side Scripting

Free download pdf