2007-08-15 11:28:22 -03:00
|
|
|
:mod:`poplib` --- POP3 protocol client
|
|
|
|
======================================
|
|
|
|
|
|
|
|
.. module:: poplib
|
|
|
|
:synopsis: POP3 protocol client (requires sockets).
|
2016-06-11 16:02:54 -03:00
|
|
|
|
Merged revisions 59605-59624 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59606 | georg.brandl | 2007-12-29 11:57:00 +0100 (Sat, 29 Dec 2007) | 2 lines
Some cleanup in the docs.
........
r59611 | martin.v.loewis | 2007-12-29 19:49:21 +0100 (Sat, 29 Dec 2007) | 2 lines
Bug #1699: Define _BSD_SOURCE only on OpenBSD.
........
r59612 | raymond.hettinger | 2007-12-29 23:09:34 +0100 (Sat, 29 Dec 2007) | 1 line
Simpler documentation for itertools.tee(). Should be backported.
........
r59613 | raymond.hettinger | 2007-12-29 23:16:24 +0100 (Sat, 29 Dec 2007) | 1 line
Improve docs for itertools.groupby(). The use of xrange(0) to create a unique object is less obvious than object().
........
r59620 | christian.heimes | 2007-12-31 15:47:07 +0100 (Mon, 31 Dec 2007) | 3 lines
Added wininst-9.0.exe executable for VS 2008
Integrated bdist_wininst into PCBuild9 directory
........
r59621 | christian.heimes | 2007-12-31 15:51:18 +0100 (Mon, 31 Dec 2007) | 1 line
Moved PCbuild directory to PC/VS7.1
........
r59622 | christian.heimes | 2007-12-31 15:59:26 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot
........
r59623 | christian.heimes | 2007-12-31 16:02:41 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot, part 2
........
r59624 | christian.heimes | 2007-12-31 16:18:55 +0100 (Mon, 31 Dec 2007) | 1 line
Renamed PCBuild9 directory to PCBuild
........
2007-12-31 12:14:33 -04:00
|
|
|
.. sectionauthor:: Andrew T. Csillag
|
|
|
|
.. revised by ESR, January 2000
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-01-27 16:38:46 -04:00
|
|
|
**Source code:** :source:`Lib/poplib.py`
|
|
|
|
|
2016-06-11 16:02:54 -03:00
|
|
|
.. index:: pair: POP3; protocol
|
|
|
|
|
2011-01-27 16:38:46 -04:00
|
|
|
--------------
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
This module defines a class, :class:`POP3`, which encapsulates a connection to a
|
2012-11-23 15:13:48 -04:00
|
|
|
POP3 server and implements the protocol as defined in :rfc:`1939`. The
|
|
|
|
:class:`POP3` class supports both the minimal and optional command sets from
|
2014-10-30 18:25:41 -03:00
|
|
|
:rfc:`1939`. The :class:`POP3` class also supports the ``STLS`` command introduced
|
2012-11-23 15:13:48 -04:00
|
|
|
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.
|
|
|
|
|
2022-08-02 16:00:41 -03:00
|
|
|
.. include:: ../includes/wasm-notavail.rst
|
|
|
|
|
2012-11-18 13:37:02 -04:00
|
|
|
The :mod:`poplib` module provides two classes:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2009-09-02 17:34:52 -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
|
|
|
|
2019-06-27 14:47:59 -03:00
|
|
|
.. audit-event:: poplib.connect self,host,port poplib.POP3
|
2019-06-24 12:42:54 -03:00
|
|
|
|
2019-06-27 14:47:59 -03:00
|
|
|
.. audit-event:: poplib.putline self,line poplib.POP3
|
|
|
|
|
|
|
|
All commands will raise an :ref:`auditing event <auditing>`
|
|
|
|
``poplib.putline`` with arguments ``self`` and ``line``,
|
|
|
|
where ``line`` is the bytes about to be sent to the remote host.
|
2019-06-24 12:42:54 -03:00
|
|
|
|
2020-01-10 10:34:05 -04:00
|
|
|
.. versionchanged:: 3.9
|
|
|
|
If the *timeout* parameter is set to be zero, it will raise a
|
|
|
|
:class:`ValueError` to prevent the creation of a non-blocking socket.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2022-12-28 15:43:19 -04:00
|
|
|
.. class:: POP3_SSL(host, port=POP3_SSL_PORT, *, 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
|
2014-03-22 14:19:11 -03:00
|
|
|
port is used. *timeout* works as in the :class:`POP3` constructor.
|
|
|
|
*context* is an optional :class:`ssl.SSLContext` object which allows
|
|
|
|
bundling SSL configuration options, certificates and private keys into a
|
|
|
|
single (potentially long-lived) structure. Please read :ref:`ssl-security`
|
|
|
|
for best practices.
|
|
|
|
|
2019-06-27 14:47:59 -03:00
|
|
|
.. audit-event:: poplib.connect self,host,port poplib.POP3_SSL
|
|
|
|
|
2020-10-19 11:52:42 -03:00
|
|
|
.. audit-event:: poplib.putline self,line poplib.POP3_SSL
|
2019-06-24 12:42:54 -03:00
|
|
|
|
2019-06-27 14:47:59 -03:00
|
|
|
All commands will raise an :ref:`auditing event <auditing>`
|
|
|
|
``poplib.putline`` with arguments ``self`` and ``line``,
|
|
|
|
where ``line`` is the bytes about to be sent to the remote host.
|
2019-06-24 12:42:54 -03:00
|
|
|
|
2010-08-17 13:09:53 -03:00
|
|
|
.. versionchanged:: 3.2
|
|
|
|
*context* parameter added.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2013-12-02 15:10:50 -04:00
|
|
|
.. versionchanged:: 3.4
|
|
|
|
The class now supports hostname check with
|
2014-03-22 14:19:11 -03:00
|
|
|
:attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see
|
|
|
|
:data:`ssl.HAS_SNI`).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2020-01-10 10:34:05 -04:00
|
|
|
.. versionchanged:: 3.9
|
|
|
|
If the *timeout* parameter is set to be zero, it will raise a
|
|
|
|
:class:`ValueError` to prevent the creation of a non-blocking socket.
|
|
|
|
|
2022-12-28 15:43:19 -04:00
|
|
|
.. versionchanged:: 3.12
|
2023-06-06 15:14:06 -03:00
|
|
|
The deprecated *keyfile* and *certfile* parameters have been removed.
|
2022-12-28 15:43:19 -04:00
|
|
|
|
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
|
|
|
|
------------
|
|
|
|
|
2021-05-28 17:54:25 -03:00
|
|
|
All POP3 commands are represented by methods of the same name, in lowercase;
|
2007-08-15 11:28:22 -03:00
|
|
|
most return the response text sent by the server.
|
|
|
|
|
2021-12-23 05:17:31 -04:00
|
|
|
A :class:`POP3` instance has the following methods:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. 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.
|
|
|
|
|
|
|
|
|
2012-11-23 15:07:39 -04:00
|
|
|
.. 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
|
2012-12-26 11:47:03 -04:00
|
|
|
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.
|
|
|
|
|
|
|
|
|
2009-09-02 17:34:52 -03:00
|
|
|
.. 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)``.
|
|
|
|
|
2015-05-16 16:05:53 -03:00
|
|
|
|
|
|
|
.. method:: POP3.utf8()
|
|
|
|
|
2015-05-18 19:38:05 -03:00
|
|
|
Try to switch to UTF-8 mode. Returns the server response if successful,
|
2015-05-16 16:05:53 -03:00
|
|
|
raises :class:`error_proto` if not. Specified in :RFC:`6856`.
|
|
|
|
|
|
|
|
.. versionadded:: 3.5
|
|
|
|
|
|
|
|
|
2012-11-23 15:13:48 -04:00
|
|
|
.. 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
|
2014-03-22 14:19:11 -03:00
|
|
|
a single (potentially long-lived) structure. Please read :ref:`ssl-security`
|
|
|
|
for best practices.
|
|
|
|
|
|
|
|
This method supports hostname checking via
|
|
|
|
:attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see
|
|
|
|
:data:`ssl.HAS_SNI`).
|
2012-11-23 15:13:48 -04:00
|
|
|
|
|
|
|
.. 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]:
|
2007-09-04 04:15:32 -03:00
|
|
|
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.
|