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

(yzsuai) #1

any machine with a network link, I was able to check my mail quickly and easily from
anywhere on the planet. Given that I make my living traveling around the world teach-
ing Python classes, this wild accessibility was a big win.


As with website maintenance, times have changed on this front. Somewhere along the
way, most ISPs began offering web-based email access with similar portability and
dropped Telnet altogether. When my ISP took away Telnet access, however, they also
took away one of my main email access methods. Luckily, Python came to the rescue
again—by writing email access scripts in Python, I could still read and send email from
any machine in the world that has Python and an Internet connection. Python can be
as portable a solution as Telnet, but much more powerful.


Moreover, I can still use these scripts as an alternative to tools suggested by the ISP.
Besides my not being fond of delegating control to commercial products of large com-
panies, closed email tools impose choices on users that are not always ideal and may
sometimes fail altogether. In many ways, the motivation for coding Python email scripts
is the same as it was for the larger GUIs in Chapter 11: the scriptability of Python
programs can be a decided advantage.


For example, Microsoft Outlook historically and by default has preferred to download
mail to your PC and delete it from the mail server as soon as you access it. This keeps
your email box small (and your ISP happy), but it isn’t exactly friendly to people who
travel and use multiple machines along the way—once accessed, you cannot get to a
prior email from any machine except the one to which it was initially downloaded.
Worse, the web-based email interfaces offered by my ISPs have at times gone offline
completely, leaving me cut off from email (and usually at the worst possible time).


The next two scripts represent one first-cut solution to such portability and reliability
constraints (we’ll see others in this and later chapters). The first, popmail.py, is a simple
mail reader tool, which downloads and prints the contents of each email in an email
account. This script is admittedly primitive, but it lets you read your email on any
machine with Python and sockets; moreover, it leaves your email intact on the server,
and isn’t susceptible to webmail outages. The second, smtpmail.py, is a one-shot script
for writing and sending a new email message that is as portable as Python itself.


Later in this chapter, we’ll implement an interactive console-based email client (py-
mail), and later in this book we’ll code a full-blown GUI email tool (PyMailGUI) and
a web-based email program of our own (PyMailCGI). For now, we’ll start with the
basics.


Mail Configuration Module


Before we get to the scripts, let’s first take a look at a common module they import and
use. The module in Example 13-17 is used to configure email parameters appropriately
for a particular user. It’s simply a collection of assignments to variables used by mail
programs that appear in this book; each major mail client has its own version, to allow


902 | Chapter 13: Client-Side Scripting

Free download pdf