cpython/Doc/library/poplib.rst

234 lines
7.4 KiB
ReStructuredText
Raw Normal View History

2007-08-15 11:28:22 -03:00
:mod:`poplib` --- POP3 protocol client
======================================
.. module:: poplib
:synopsis: POP3 protocol client (requires sockets).
.. sectionauthor:: Andrew T. Csillag
.. revised by ESR, January 2000
2007-08-15 11:28:22 -03:00
.. index:: pair: POP3; protocol
2011-01-27 16:38:46 -04:00
**Source code:** :source:`Lib/poplib.py`
--------------
2007-08-15 11:28:22 -03:00
This module defines a class, :class:`POP3`, which encapsulates a connection to a
POP3 server and implements the protocol as defined in :rfc:`1939`. The
:class:`POP3` class supports both the minimal and optional command sets from
:rfc:`1939`. The :class:`POP3` class also supports the `STLS` command introduced
in :rfc:`2595` to enable encrypted communication on an already established connection.
2007-08-15 11:28:22 -03:00
Additionally, this module provides a class :class:`POP3_SSL`, which provides
support for connecting to POP3 servers that use SSL as an underlying protocol
layer.
Note that POP3, though widely supported, is obsolescent. The implementation
quality of POP3 servers varies widely, and too many are quite poor. If your
mailserver supports IMAP, you would be better off using the
:class:`imaplib.IMAP4` class, as IMAP servers tend to be better implemented.
The :mod:`poplib` module provides two classes:
2007-08-15 11:28:22 -03:00
.. class:: POP3(host, port=POP3_PORT[, timeout])
2007-08-15 11:28:22 -03:00
This class implements the actual POP3 protocol. The connection is created when
the instance is initialized. If *port* is omitted, the standard POP3 port (110)
is used. The optional *timeout* parameter specifies a timeout in seconds for the
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63562 | martin.v.loewis | 2008-05-23 17:06:50 +0200 (Fri, 23 May 2008) | 2 lines Patch #1722225: Support QNX 6. ........ r63570 | trent.nelson | 2008-05-23 22:33:14 +0200 (Fri, 23 May 2008) | 1 line Introduce a user macro named $(externalsDir), which should point to the root directory of where all the external sources should live. Developers can change this value if their external sources live elsewhere. The default of '..\..' matches the current status quo. ........ r63728 | gregory.p.smith | 2008-05-26 23:16:34 +0200 (Mon, 26 May 2008) | 4 lines Fix issue2589: there was a potential integer overflow leading to memory corruption on esoteric platforms and incorrect behavior on normal platforms. ........ r63734 | gregory.p.smith | 2008-05-27 00:07:28 +0200 (Tue, 27 May 2008) | 3 lines Fix issue2588: Do not execute str[size-1] = '\0' when a 0 size is passed in. (The assert won't prevent this in non-debug builds). ........ r63784 | raymond.hettinger | 2008-05-29 10:38:23 +0200 (Thu, 29 May 2008) | 1 line Fix two typos. ........ r63788 | facundo.batista | 2008-05-29 18:39:26 +0200 (Thu, 29 May 2008) | 6 lines Fixed the semantic of timeout for socket.create_connection and all the upper level libraries that use it, including urllib2. Added and fixed some tests, and changed docs correspondingly. Thanks to John J Lee for the patch and the pusing, :) ........ r63802 | mark.dickinson | 2008-05-30 04:46:53 +0200 (Fri, 30 May 2008) | 2 lines Fix typo in testSum ........ r63817 | raymond.hettinger | 2008-05-30 20:20:50 +0200 (Fri, 30 May 2008) | 8 lines * Mark intermedidate computes values (hi, lo, yr) as volatile. * Expand comments. * Swap variable names in the sum_exact code so that x and y are consistently chosen as the larger and smaller magnitude values respectively. ........ r63827 | raymond.hettinger | 2008-05-31 05:24:31 +0200 (Sat, 31 May 2008) | 1 line Implement heapq in terms of less-than (to match list.sort()). ........ r63839 | gerhard.haering | 2008-05-31 23:33:27 +0200 (Sat, 31 May 2008) | 2 lines Fixed rowcount for SELECT statements. They're -1 now (again), for better DB-API 2.0 compliance. ........ r63887 | gregory.p.smith | 2008-06-02 06:05:52 +0200 (Mon, 02 Jun 2008) | 4 lines Fix issue 2782: be less strict about the format string type in strftime. Accept unicode and anything else ParseTuple "s#" can deal with. This matches the time.strftime behavior. ........ r63975 | neal.norwitz | 2008-06-06 06:47:01 +0200 (Fri, 06 Jun 2008) | 3 lines Aldo Cortesi confirmed this is still needed for OpenBSD 4.2 and 4.3. (I didn't regen configure, since I don't have a working autoconf.) ........ r63998 | raymond.hettinger | 2008-06-06 23:47:51 +0200 (Fri, 06 Jun 2008) | 1 line Issue 3501: Make heapq support both __le__ and __lt__. ........
2008-06-10 14:40:04 -03:00
connection attempt (if not specified, the global default timeout setting will
be used).
2007-08-15 11:28:22 -03:00
.. class:: POP3_SSL(host, port=POP3_SSL_PORT, keyfile=None, certfile=None, timeout=None, context=None)
2007-08-15 11:28:22 -03:00
This is a subclass of :class:`POP3` that connects to the server over an SSL
encrypted socket. If *port* is not specified, 995, the standard POP3-over-SSL
port is used. *keyfile* and *certfile* are also optional - they can contain a
PEM formatted private key and certificate chain file for the SSL connection.
*timeout* works as in the :class:`POP3` constructor. *context* parameter is a
:class:`ssl.SSLContext` object which allows bundling SSL configuration
options, certificates and private keys into a single (potentially long-lived)
structure.
.. versionchanged:: 3.2
*context* parameter added.
2007-08-15 11:28:22 -03:00
.. versionchanged:: 3.4
The class now supports hostname check with
:attr:`SSLContext.check_hostname` and *Server Name Indicator* (see
:data:`~ssl.HAS_SNI`).
2007-08-15 11:28:22 -03:00
One exception is defined as an attribute of the :mod:`poplib` module:
.. exception:: error_proto
Exception raised on any errors from this module (errors from :mod:`socket`
module are not caught). The reason for the exception is passed to the
constructor as a string.
.. seealso::
Module :mod:`imaplib`
The standard Python IMAP module.
`Frequently Asked Questions About Fetchmail <http://www.catb.org/~esr/fetchmail/fetchmail-FAQ.html>`_
The FAQ for the :program:`fetchmail` POP/IMAP client collects information on
POP3 server variations and RFC noncompliance that may be useful if you need to
write an application based on the POP protocol.
.. _pop3-objects:
POP3 Objects
------------
All POP3 commands are represented by methods of the same name, in lower-case;
most return the response text sent by the server.
An :class:`POP3` instance has the following methods:
.. method:: POP3.set_debuglevel(level)
Set the instance's debugging level. This controls the amount of debugging
output printed. The default, ``0``, produces no debugging output. A value of
``1`` produces a moderate amount of debugging output, generally a single line
per request. A value of ``2`` or higher produces the maximum amount of
debugging output, logging each line sent and received on the control connection.
.. method:: POP3.getwelcome()
Returns the greeting string sent by the POP3 server.
.. method:: POP3.capa()
Query the server's capabilities as specified in :rfc:`2449`.
Returns a dictionary in the form ``{'name': ['param'...]}``.
.. versionadded:: 3.4
2007-08-15 11:28:22 -03:00
.. method:: POP3.user(username)
Send user command, response should indicate that a password is required.
.. method:: POP3.pass_(password)
Send password, response includes message count and mailbox size. Note: the
mailbox on the server is locked until :meth:`~poplib.quit` is called.
2007-08-15 11:28:22 -03:00
.. method:: POP3.apop(user, secret)
Use the more secure APOP authentication to log into the POP3 server.
.. method:: POP3.rpop(user)
Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server.
.. method:: POP3.stat()
Get mailbox status. The result is a tuple of 2 integers: ``(message count,
mailbox size)``.
.. method:: POP3.list([which])
Request message list, result is in the form ``(response, ['mesg_num octets',
...], octets)``. If *which* is set, it is the message to list.
.. method:: POP3.retr(which)
Retrieve whole message number *which*, and set its seen flag. Result is in form
``(response, ['line', ...], octets)``.
.. method:: POP3.dele(which)
Flag message number *which* for deletion. On most servers deletions are not
actually performed until QUIT (the major exception is Eudora QPOP, which
deliberately violates the RFCs by doing pending deletes on any disconnect).
.. method:: POP3.rset()
Remove any deletion marks for the mailbox.
.. method:: POP3.noop()
Do nothing. Might be used as a keep-alive.
.. method:: POP3.quit()
Signoff: commit changes, unlock mailbox, drop connection.
.. method:: POP3.top(which, howmuch)
Retrieves the message header plus *howmuch* lines of the message after the
header of message number *which*. Result is in form ``(response, ['line', ...],
octets)``.
The POP3 TOP command this method uses, unlike the RETR command, doesn't set the
message's seen flag; unfortunately, TOP is poorly specified in the RFCs and is
frequently broken in off-brand servers. Test this method by hand against the
POP3 servers you will use before trusting it.
.. method:: POP3.uidl(which=None)
2007-08-15 11:28:22 -03:00
Return message digest (unique id) list. If *which* is specified, result contains
the unique id for that message in the form ``'response mesgnum uid``, otherwise
result is list ``(response, ['mesgnum uid', ...], octets)``.
.. method:: POP3.stls(context=None)
Start a TLS session on the active connection as specified in :rfc:`2595`.
This is only allowed before user authentication
*context* parameter is a :class:`ssl.SSLContext` object which allows
bundling SSL configuration options, certificates and private keys into
a single (potentially long-lived) structure. This method supports
hostname checking via :attr:`SSLContext.check_hostname`
:attr:`SSLContext.check_hostname` and *Server Name Indicator* (see
:data:`~ssl.HAS_SNI`).
.. versionadded:: 3.4
2007-08-15 11:28:22 -03:00
Instances of :class:`POP3_SSL` have no additional methods. The interface of this
subclass is identical to its parent.
.. _pop3-example:
POP3 Example
------------
Here is a minimal example (without error checking) that opens a mailbox and
retrieves and prints all messages::
import getpass, poplib
M = poplib.POP3('localhost')
M.user(getpass.getuser())
M.pass_(getpass.getpass())
numMessages = len(M.list()[1])
for i in range(numMessages):
for j in M.retr(i+1)[1]:
print(j)
2007-08-15 11:28:22 -03:00
At the end of the module, there is a test section that contains a more extensive
example of usage.