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

(yzsuai) #1
>>> import urllib.parse
>>> urllib.parse.quote_plus("There's bugger all down here on Earth")
'There%27s+bugger+all+down+here+on+Earth'

The module commonhtml, though, calls the higher-level urllib.parse.urlencode func-
tion, which translates a dictionary of name:value pairs into a complete URL query pa-
rameter string, ready to add after a? marker in a URL. For instance, here is
urlencode in action at the interactive prompt:


>>> parmdict = {'user': 'Brian',
... 'pswd': '#!/spam',
... 'text': 'Say no more, squire!'}

>>> urllib.parse.urlencode(parmdict)
'text=Say+no+more%2C+squire%21&pswd=%23%21%2Fspam&user=Brian'

>>> "%s?%s" % ("http://scriptname.py", urllib.parse.urlencode(parmdict))
'http://scriptname.py?text=Say+no+more%2C+squire%21&pswd=%23%21%2Fspam&user=Brian'

Internally, urlencode passes each name and value in the dictionary to the built-in str
function (to make sure they are strings), and then runs each one through url
lib.parse.quote_plus as they are added to the result. The CGI script builds up a list of
similar dictionaries and passes it to commonhtml to be formatted into a selection list
page.†


In broader terms, generating URLs with parameters like this is one way to pass state
information to the next script (along with cookies, hidden form input fields, and server
databases, discussed in Chapter 15). Without such state information, users would have
to reenter the username, password, and site name on every page they visit along the way.


Incidentally, the list generated by this script is not radically different in functionality
from what we built in the PyMailGUI program in Chapter 14, though the two differ
cosmetically. Figure 16-11 shows this strictly client-side GUI’s view on the same email
list displayed in Figure 16-8.


It’s important to keep in mind that PyMailGUI uses the tkinter GUI library to build up
a user interface instead of sending HTML to a browser. It also runs entirely on the client
and talks directly to email servers, downloading mail from the POP server to the client
machine over sockets on demand. Because it retains memory for the duration of the
session, PyMailGUI can easily minimize mail server access. After the initial header load,
it needs to load only newly arrived email headers on subsequent load requests. More-
over, it can update its email index in-memory on deletions instead of reloading anew
from the server, and it has enough state to perform safe deletions of messages that check
for server inbox matches. PyMailGUI also remembers emails you’ve already viewed—
they need not be reloaded again while the program runs.


† Technically, again, you should generally escape & separators in generated URL links by running the URL
through cgi.escape, if any parameter’s name could be the same as that of an HTML character escape code
(e.g., &=high). See Chapter 15 for more details; they aren’t escaped here because there are no clashes
between URL and HTML.


1256 | Chapter 16: The PyMailCGI Server

Free download pdf