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

(yzsuai) #1
But because CGI scripts are run by the user “nobody,” PYTHONPATH may be arbitrary
when a CGI script runs.
Before you puzzle over this too hard, you should know that this is often not a
concern in practice. Because Python usually searches the current directory for im-
ported modules by default, this is not an issue if all of your scripts and any modules
and packages they use are stored in your web directory, and your web server
launches CGI scripts in the directory in which they reside. But if the module lives
elsewhere, you may need to modify the sys.path list in your scripts to adjust the
search path manually before imports—for instance, with sys.path.append(dir
name) calls, index assignments, and so on.

End-of-line conventions (optional)
On some Unix (and Linux) servers, you might also have to make sure that your
script text files follow the Unix end-of-line convention (\n), not DOS (\r\n). This
isn’t an issue if you edit and debug right on the server (or on another Unix machine)
or FTP files one by one in text mode. But if you edit and upload your scripts from
a PC to a Unix server in a tar file (or in FTP binary mode), you may need to convert
end-of-lines after the upload. For instance, the server that was used for the second
edition of this text returns a default error page for scripts whose end-of-lines are
in DOS format. See Chapter 6 for techniques and a note on automated end-of-line
converter scripts.


Unbuffered output streams (optional)
Under some servers, the print call statement may buffer its output. If you have a
long-running CGI script, to avoid making the user wait to see results, you may wish
to manually flush your printed text (call sys.stdout.flush()) or run your Python
scripts in unbuffered mode. Recall from Chapter 5 that you can make streams
unbuffered by running with the -u command-line flag or by setting your
PYTHONUNBUFFERED environment variable to a nonempty value.
To use -u in the CGI world, try using a first line on Unix-like platforms like #!/
usr/bin/python -u. In typical usage, output buffering is not usually a factor. On
some servers and clients, though, this may be a resolution for empty reply pages,
or premature end-of-script header errors—the client may time out before the buf-
fered output stream is sent (though more commonly, these cases reflect genuine
program errors in your script).


This installation process may sound a bit complex at first glance, but much of it is
server-dependent, and it’s not bad once you’ve worked through it on your own. It’s
only a concern at install time and can usually be automated to some extent with Python
scripts run on the server. To summarize, most Python CGI scripts are text files of Python
code, which:



  • Are named according to your web server’s conventions (e.g., file.py)

  • Are stored in a directory recognized by your web server (e.g., cgi-bin/)

  • Are given executable file permissions if required (e.g., chmod 755 file.py)


1144 | Chapter 15: Server-Side Scripting

Free download pdf