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

(yzsuai) #1

exactly as if we had done so manually. It shows up at the end of the URL in the response
page’s address field, even though we really typed it into a form. Clicking the link at the
bottom of Figure 15-29 opens the file-getter script’s source in the same way, though
the URL is explicit.‡


Handling private files and errors


As long as CGI scripts have permission to open the desired server-side file, this script
can be used to view and locally save any file on the server. For instance, Figure 15-30
shows the page we’re served after asking for the file path PyMailCgi/pymailcgi.html—
an HTML text file in another application’s subdirectory, nested within the parent di-
rectory of this script (we explore PyMailCGI in the next chapter). Users can specify
both relative and absolute paths to reach a file—any path syntax the server understands
will do.


Figure 15-30. Viewing files with relative paths


More generally, this script will display any file path for which the username under which
the CGI script runs has read access. On some servers, this is often the user “nobody”—
a predefined username with limited permissions. Just about every server-side file used
in web applications will be accessible, though, or else they couldn’t be referenced from
browsers in the first place. When running our local web server, every file on the com-
puter can be inspected: C:\Users\mark\Stuff\Websites\public_html\index.html works
fine when entered in the form of Figure 15-29 on my laptop, for example.


‡ You may notice another difference in the response pages produced by the form and an explicitly typed URL:
for the form, the value of the “filename” parameter at the end of the URL in the response may contain URL
escape codes for some characters in the file path you typed. Browsers automatically translate some non-ASCII
characters into URL escapes (just like urllib.parse.quote). URL escapes were discussed earlier in this
chapter; we’ll see an example of this automatic browser escaping at work in an upcoming screenshot.


Transferring Files to Clients and Servers | 1215
Free download pdf