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

(yzsuai) #1

limitations, here is what one of those link lines looks like, reformatted with line breaks
and spaces to make it easier to understand:


<tr><th><a href="onViewListLink.py?
pswd=wtGmpsjeb7359&
mnum=5&
user=PP4E%40learning-python.com&
site=pop.secureserver.net">View</a>
<td>Among our weapons are these | [email protected] | Fri, 07 May 2010 20:32...

PyMailCGI generates relative minimal URLs (server and pathname values come from
the prior page, unless set in commonhtml). Clicking on the word View in the hyperlink
rendered from this HTML code triggers the onViewListLink script as usual, passing it
all the parameters embedded at the end of the URL: the POP username, the POP mes-
sage number of the message associated with this link, and the POP password and site
information. These values will be available in the object returned by
cgi.FieldStorage in the next script run. Note that the mnum POP message number pa-
rameter differs in each link because each opens a different message when clicked and
that the text after comes from message headers extracted by the mailtools package,
using the email package.


The commonhtml module escapes all of the link parameters with the urllib.parse mod-
ule, not cgi.escape, because they are part of a URL. This can matter in the pswd pass-
word parameter—its value might be encrypted and arbitrary bytes, but urllib.parse
additionally escapes nonsafe characters in the encrypted string per URL convention (it
translates to %xx character sequences). It’s OK if the encryptor yields odd—even non-
printable—characters because URL encoding makes them legible for transmission.
When the password reaches the next script, cgi.FieldStorage undoes URL escape se-
quences, leaving the encrypted password string without % escapes.


It’s instructive to see how commonhtml builds up the stateful link parameters. Earlier, we
learned how to use the urllib.parse.quote_plus call to escape a string for inclusion in
URLs:


Figure 16-10. PyMailCGI view list, generated HTML


Reading POP Email| 1255
Free download pdf