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

(yzsuai) #1
b''
b'I cut down trees, I skip and jump,'
b'I like to press wild flowers...'
b''

As we’ll see later, we’ll need to decode similarly in order to parse this text with email
tools. The next section exposes the bytes-based interface as well.


Fetching Email at the Interactive Prompt


If you don’t mind typing code and reading POP server messages, it’s possible to use the
Python interactive prompt as a simple email client, too. The following session uses two
additional interfaces we’ll apply in later examples:


conn.list()
Returns a list of “message-number message-size” strings.


conn.top( N , 0)
Retrieves just the header text portion of message number N.


The top call also returns a tuple that includes the list of line strings sent back; its second
argument tells the server how many additional lines after the headers to send, if any. If
all you need are header details, top can be much quicker than the full text fetch of
retr, provided your mail server implements the TOP command (most do):


C:\...\PP4E\Internet\Email> python
>>> from poplib import POP3
>>> conn = POP3('pop.secureserver.net') # connect to server
>>> conn.user('[email protected]') # log in to account
b'+OK '
>>> conn.pass_('xxxxxxxx')
b'+OK '

>>> conn.stat() # num mails, num bytes
(2, 3268)
>>> conn.list()
(b'+OK ', [b'1 1860', b'2 1408'], 16)

>>> conn.top(1, 0)
(b'+OK 1860 octets ', [b'Received: (qmail 7690 invoked from network); 5 May 2010
...lines omitted...
b'X-Originating-IP: 209.86.224.51', b'X-Nonspam: None', b'', b''], 1827)

>>> conn.retr(1)
(b'+OK 1860 octets ', [b'Received: (qmail 7690 invoked from network); 5 May 2010
...lines omitted...
b'X-Originating-IP: 209.86.224.51', b'X-Nonspam: None', b'',
b'I cut down trees, I skip and jump,', b'I like to press wild flowers...',
b'', b''], 1898)

>>> conn.quit()
b'+OK '

POP: Fetching Email | 909
Free download pdf