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

(yzsuai) #1

Even so, the list page’s headers-only download can be slow if you have many messages
in your inbox (and as I confessed in Chapter 14, I have thousands in one of mine). A
better solution would somehow cache mails to limit reloads, at least for the duration
of a browser session. For example, we might load headers of only newly arrived mes-
sages, and cache headers of mails already fetched, as done in the PyMailGUI client of
Chapter 14.


Due to the lack of state retention in CGI scripts, though, this would likely require some
sort of server-side database. We might, for instance, store already fetched message
headers under a generated key that identifies the session (e.g., with process number
and time) and pass that key between pages as a cookie, hidden form field, or URL query
parameter. Each page would use the key to fetch cached mail stored directly on the web
server, instead of loading it from the email server again. Presumably, loading from a
local cache file would be faster than loading from a network connection to the mail
server.


This would make for an interesting exercise, too, if you wish to extend this system on
your own, but it would also result in more pages than this chapter has to spend (frankly,
I ran out of time for this project and real estate in this chapter long before I ran out of
potential enhancements).


Presentation Overview


Much of the “action” in PyMailCGI is encapsulated in shared utility modules, especially
one called commonhtml.py. As you’ll see in a moment, the CGI scripts that implement
user interaction don’t do much by themselves because of this. This architecture was
chosen deliberately, to make scripts simple, avoid code redundancy, and implement a
common look-and-feel in shared code. But it means you must jump between files to
understand how the whole system works.


To make this example easier to digest, we’re going to explore its code in two chunks:
page scripts first, and then the utility modules. First, we’ll study screenshots of the
major web pages served up by the system and the HTML files and top-level Python
CGI scripts used to generate them. We begin by following a send mail interaction, and
then trace how existing email is read and then processed. Most implementation details
will be presented in these sections, but be sure to flip ahead to the utility modules listed
later to understand what the scripts are really doing.


I should also point out that this is a fairly complex system, and I won’t describe it in
exhaustive detail; as for PyMailGUI and Chapter 14, be sure to read the source code
along the way for details not made explicit in the narrative. All of the system’s source
code appears in this chapter, as well as in the book’s examples distribution package,
and we will study its key concepts here. But as usual with case studies in this book, I
assume that you can read Python code by now and that you will consult the example’s
source code for more details. Because Python’s syntax is so close to “executable


1236 | Chapter 16: The PyMailCGI Server

Free download pdf