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

(yzsuai) #1

statement generates an extra line feed in addition to the one that the print generates
itself. The net effect is to insert a blank line after the header line. The rest of this pro-
gram’s output is standard HTML and is used by the browser to generate a web page
on a client, exactly as if the HTML lived in a static HTML file on the server.‡


CGI scripts are accessed just like HTML files: you either type the full URL of this script
into your browser’s address field or click on the tutor0.py link line in the examples root
page of Figure 15-1 (which follows a minimal hyperlink that resolves to the script’s full
URL). Figure 15-3 shows the result page generated if you point your browser at this
script.


Figure 15-3. A simple web page from a CGI script


Installing CGI scripts


If you are running the local web server described at the start of this chapter, no extra
installation steps are required to make this example work, and you can safely skip most
of this section. If you want to put CGI scripts on another server, though, there are a
few pragmatic details you may need to know about. This section provides a brief over-
view of common CGI configuration details for reference.


Like HTML files, CGI scripts are simple text files that you can either create on your
local machine and upload to the server by FTP or write with a text editor running
directly on the server machine (perhaps using a Telnet or SSH client). However, because
CGI scripts are run as programs, they have some unique installation requirements that
differ from simple HTML files. In particular, they usually must be stored and named
specially, and they must be configured as programs that are executable by arbitrary
users. Depending on your needs, CGI scripts also may require help finding imported


‡ Notice that the script does not generate the enclosing and tags included in the static HTML
file of the prior section. Strictly speaking, it should—HTML without such tags is technically invalid. But
because all commonly used browsers simply ignore the omission, we’ll take some liberties with HTML syntax
in this book. If you need to care about such things, consult HTML references for more formal details.


1142 | Chapter 15: Server-Side Scripting

Free download pdf