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

(yzsuai) #1
This user interface is about as simple as it could be—after connecting to the server, it
prints the complete and raw full text of one message at a time, pausing between each
until you press the Enter key. The input built-in is called to wait for the key press
between message displays. The pause keeps messages from scrolling off the screen too
fast; to make them visually distinct, emails are also separated by lines of dashes.
We could make the display fancier (e.g., we can use the email p a c k a g e t o p a r s e h e a d e r s ,
bodies, and attachments—watch for examples in this and later chapters), but here we
simply display the whole message that was sent. This works well for simple mails like
these two, but it can be inconvenient for larger messages with attachments; we’ll im-
prove on this in later clients.
This book won’t cover the full of set of headers that may appear in emails, but we’ll
make use of some along the way. For example, the X-Mailer header line, if present,
typically identifies the sending program; we’ll use it later to identify Python-coded email
senders we write. The more common headers such as From a n d Subject a r e m o r e c r u c i a l
to a message. In fact, a variety of extra header lines can be sent in a message’s text. The
Received headers, for example, trace the machines that a message passed through on
its way to the target mailbox.
Because popmail prints the entire raw text of a message, you see all headers here, but
you usually see only a few by default in end-user-oriented mail GUIs such as Outlook
and webmail pages. The raw text here also makes apparent the email structure we noted
earlier: an email in general consists of a set of headers like those here, followed by a
blank line, which is followed by the mail’s main text, though as we’ll see later, they can
be more complex if there are alternative parts or attachments.
The script in Example 13-18 never deletes mail from the server. Mail is simply retrieved
and printed and will be shown again the next time you run the script (barring deletion
in another tool, of course). To really remove mail permanently, we need to call other
methods (e.g., server.dele(msgnum)), but such a capability is best deferred until we
develop more interactive mail tools.
Notice how the reader script decodes each mail content line with line.decode into a
str string for display; as mentioned earlier, poplib returns content as bytes strings in
3.X. In fact, if we change the script to not decode, this becomes more obvious in its
output:
[Press Enter key]
...assorted lines omitted...
b'Date: Wed, 5 May 2010 11:29:36 −0400 (EDT)'
b'From: [email protected]'
b'Reply-To: [email protected]'
b'To: [email protected]'
b"Subject: I'm a Lumberjack, and I'm Okay"
b'Mime-Version: 1.0'
b'Content-Type: text/plain; charset=UTF-8'
b'Content-Transfer-Encoding: 7bit'
b'X-Mailer: EarthLink Zoo Mail 1.0'

908 | Chapter 13: Client-Side Scripting

Do


wnload from Wow! eBook <www.wowebook.com>

Free download pdf