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

(yzsuai) #1

the file type. For instance, clicking on the “.txt” file will likely open it in either the
browser or a text editor. In other mails, clicking on “.jpg” files may open an image
viewer, “.pdf” may open Adobe Reader, and so on. Figure 16-13 shows the result of
clicking the “.py” attachment part of Figure 16-12’s message in Chrome.


Passing State Information in HTML Hidden Input Fields


What you don’t see on the view page in Figure 16-12 is just as important as what you
do see. We need to defer to Example 16-14 for coding details, but something new is
going on here. The original message number, as well as the POP user and (still encoded)
password information sent to this script as part of the stateful link’s URL, wind up
being copied into the HTML used to create this view page, as the values of hidden input
fields in the form. The hidden field generation code in commonhtml looks like this:


print('<form method=post action="%s/onViewPageAction.py">' % urlroot)
print('<input type=hidden name=mnum value="%s">' % msgnum)
print('<input type=hidden name=user value="%s">' % user) # from page|url
print('<input type=hidden name=site value="%s">' % site) # for deletes
print('<input type=hidden name=pswd value="%s">' % pswd) # pswd encoded

As we’ve learned, much like parameters in generated hyperlink URLs, hidden fields in
a page’s HTML allow us to embed state information inside this web page itself. Unless
you view that page’s source, you can’t see this state information because hidden fields
are never displayed. But when this form’s Submit button is clicked, hidden field values
are automatically transmitted to the next script along with the visible fields on the form.


Figure 16-13. Attached part file link display


1262 | Chapter 16: The PyMailCGI Server

Free download pdf