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

(yzsuai) #1

From a higher perspective, URLs like these are really direct calls (albeit across the Web)
to our Python script, with filename parameters passed explicitly—we’re using the script
much like a subroutine located elsewhere in cyberspace which returns the text of a file
we wish to view. As we’ve seen, parameters passed in URLs are treated the same as field
inputs in forms; for convenience, let’s also write a simple web page that allows the
desired file to be typed directly into a form, as shown in Example 15-28.


Example 15-28. PP4E\Internet\Web\getfile.html


Getfile: download page


Type name of server file to be viewed






View script code

Figure 15-29 shows the page we receive when we visit this file’s URL. We need to type
only the filename in this page, not the full CGI script address; notice that I can use
forward slashes here because the browser will escape on transmission and Python’s
open allows either type of slash on Windows (in query parameters created manually,
it’s up to coders or generators to do the right thing).


Figure 15-29. Source code viewer selection page


When we press this page’s Download button to submit the form, the filename is trans-
mitted to the server, and we get back the same page as before, when the filename was
appended to the URL (it’s the same as Figure 15-28, albeit with a different directory
separator slash). In fact, the filename will be appended to the URL here, too; the get
method in the form’s HTML instructs the browser to append the filename to the URL,


1214 | Chapter 15: Server-Side Scripting

Free download pdf