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

(yzsuai) #1

Notice that there are no usernames or passwords to be found here; as we saw in Chap-
ter 13, SMTP usually requires only a server that listens on the SMTP port, not a user
account or password. As we also saw in that chapter, SMTP send operations that fail
either raise a Python exception (e.g., if the server host can’t be reached) or return a
dictionary of failed recipients; our mailtools package modules insulate us from these
details by always raising an exception in either case.


Error Pages


If there is a problem during mail delivery, we get an error page such as the one shown
in Figure 16-5. This page reflects a failed recipient and includes a stack trace generated
by the standard library’s traceback module. On errors Python detects, the Python error
message and extra details would be displayed.


It’s also worth pointing out that the commonhtml module encapsulates the generation of
both the confirmation and the error pages so that all such pages look the same in
PyMailCGI no matter where and when they are produced. Logic that generates the mail
edit page in commonhtml is reused by the reply and forward actions, too (but with dif-
ferent mail headers).


Common Look-and-Feel


In fact, commonhtml makes all pages look similar—it also provides common page
header (top) and footer (bottom) generation functions, which are used everywhere in
the system. You may have already noticed that all the pages so far follow the same
pattern: they start with a title and horizontal rule, have something unique in the middle,
and end with another rule, followed by a Python icon and link at the bottom. This
common look-and-feel is the product of shared code in commonhtml; it generates every-
thing but the middle section for every page in the system (except the root page, a static
HTML file).


Figure 16-4. PyMailCGI send confirmation page


1246 | Chapter 16: The PyMailCGI Server

Free download pdf