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

(yzsuai) #1
file 2009-public-classes.htm_cmp_DeepBlue100_vbtn.gif
file 2009-public-classes.htm_cmp_DeepBlue100_vbtn_p.gif
file 2010-public-classes.html_cmp_DeepBlue100_vbtn_p.gif
file 2010-public-classes.html_cmp_deepblue100_vbtn.gif
directory exited
directory exited

...lines omitted...

file priorclients.html
file public_classes.htm
file python_conf_ora.gif
file topics.html
Done: 366 files and 18 directories cleaned.

Downloading Remote Trees


It is possible to extend this remote tree-cleaner to also download a remote tree with
subdirectories: rather than deleting, as you walk the remote tree simply create a local
directory to match a remote one, and download nondirectory files. We’ll leave this final
step as a suggested exercise, though, partly because its dependence on the format pro-
duced by server directory listings makes it complex to be robust and partly because this
use case is less common for me—in practice, I am more likely to maintain a site on my
PC and upload to the server than to download a tree.


If you do wish to experiment with a recursive download, though, be sure to consult the
script Tools\Scripts\ftpmirror.py in Python’s install or source tree for hints. That script
attempts to download a remote directory tree by FTP, and allows for various directory
listing formats which we’ll skip here in the interest of space. For our purposes, it’s time
to move on to the next protocol on our tour—Internet email.


Processing Internet Email


Some of the other most common, higher-level Internet protocols have to do with read-
ing and sending email messages: POP and IMAP for fetching email from servers, SMTP
for sending new messages, and other formalisms such as RFC822 for specifying email
message content and format. You don’t normally need to know about such acronyms
when using common email tools, but internally, programs like Microsoft Outlook and
webmail systems generally talk to POP and SMTP servers to do your bidding.


Like FTP, email ultimately consists of formatted commands and byte streams shipped
over sockets and ports (port 110 for POP; 25 for SMTP). Regardless of the nature of its
content and attachments, an email message is little more than a string of bytes sent and
received through sockets. But also like FTP, Python has standard library modules to
simplify all aspects of email processing:


Processing Internet Email | 899
Free download pdf