Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{poplib} --- |
Fred Drake | e2effbd | 1999-04-22 16:21:09 +0000 | [diff] [blame] | 2 | POP3 protocol client} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | e2effbd | 1999-04-22 16:21:09 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{poplib} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{POP3 protocol client (requires sockets).} |
| 6 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 7 | %By Andrew T. Csillag |
| 8 | %Even though I put it into LaTeX, I cannot really claim that I wrote |
| 9 | %it since I just stole most of it from the poplib.py source code and |
| 10 | %the imaplib ``chapter''. |
Eric S. Raymond | 5ac9795 | 2001-01-11 04:19:52 +0000 | [diff] [blame] | 11 | %Revised by ESR, January 2000 |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 12 | |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 13 | \indexii{POP3}{protocol} |
| 14 | |
| 15 | This module defines a class, \class{POP3}, which encapsulates a |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 16 | connection to a POP3 server and implements the protocol as defined in |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 17 | \rfc{1725}. The \class{POP3} class supports both the minimal and |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 18 | optional command sets. Additionally, this module provides a class |
| 19 | \class{POP3_SSL}, which provides support for connecting to POP3 |
| 20 | servers that use SSL as an underlying protocol layer. |
| 21 | |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 22 | |
Eric S. Raymond | 5ac9795 | 2001-01-11 04:19:52 +0000 | [diff] [blame] | 23 | Note that POP3, though widely supported, is obsolescent. The |
| 24 | implementation quality of POP3 servers varies widely, and too many are |
| 25 | quite poor. If your mailserver supports IMAP, you would be better off |
Georg Brandl | 4e9165d | 2007-04-01 21:29:15 +0000 | [diff] [blame] | 26 | using the \class{\refmodule{imaplib}.IMAP4} class, as IMAP |
Fred Drake | a7c9ac6 | 2001-05-09 03:49:48 +0000 | [diff] [blame] | 27 | servers tend to be better implemented. |
Eric S. Raymond | 5ac9795 | 2001-01-11 04:19:52 +0000 | [diff] [blame] | 28 | |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 29 | A single class is provided by the \module{poplib} module: |
| 30 | |
Facundo Batista | 1b1c347 | 2007-03-27 18:23:21 +0000 | [diff] [blame] | 31 | \begin{classdesc}{POP3}{host\optional{, port\optional{, timeout}}} |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 32 | This class implements the actual POP3 protocol. The connection is |
| 33 | created when the instance is initialized. |
| 34 | If \var{port} is omitted, the standard POP3 port (110) is used. |
Facundo Batista | 1b1c347 | 2007-03-27 18:23:21 +0000 | [diff] [blame] | 35 | The optional \var{timeout} parameter specifies a timeout in seconds for the |
| 36 | connection attempt (if not specified, or passed as None, the global default |
| 37 | timeout setting will be used). |
Facundo Batista | 3ed365b | 2007-06-11 16:27:08 +0000 | [diff] [blame^] | 38 | |
| 39 | \versionchanged[\var{timeout} was added]{2.6} |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 40 | \end{classdesc} |
| 41 | |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 42 | \begin{classdesc}{POP3_SSL}{host\optional{, port\optional{, keyfile\optional{, certfile}}}} |
| 43 | This is a subclass of \class{POP3} that connects to the server over an |
| 44 | SSL encrypted socket. If \var{port} is not specified, 995, the |
| 45 | standard POP3-over-SSL port is used. \var{keyfile} and \var{certfile} |
| 46 | are also optional - they can contain a PEM formatted private key and |
| 47 | certificate chain file for the SSL connection. |
| 48 | |
| 49 | \versionadded{2.4} |
| 50 | \end{classdesc} |
| 51 | |
Fred Drake | e2effbd | 1999-04-22 16:21:09 +0000 | [diff] [blame] | 52 | One exception is defined as an attribute of the \module{poplib} module: |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 53 | |
| 54 | \begin{excdesc}{error_proto} |
Facundo Batista | c3a35e1 | 2007-04-03 14:05:08 +0000 | [diff] [blame] | 55 | Exception raised on any errors from this module (errors from |
| 56 | \module{socket} module are not caught). The reason for the exception |
| 57 | is passed to the constructor as a string. |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 58 | \end{excdesc} |
| 59 | |
Fred Drake | a7c9ac6 | 2001-05-09 03:49:48 +0000 | [diff] [blame] | 60 | \begin{seealso} |
| 61 | \seemodule{imaplib}{The standard Python IMAP module.} |
Andrew M. Kuchling | 44a9823 | 2004-01-11 23:00:16 +0000 | [diff] [blame] | 62 | \seetitle[http://www.catb.org/\~{}esr/fetchmail/fetchmail-FAQ.html] |
Fred Drake | d8eeeae | 2002-10-18 16:50:17 +0000 | [diff] [blame] | 63 | {Frequently Asked Questions About Fetchmail} |
| 64 | {The FAQ for the \program{fetchmail} POP/IMAP client collects |
| 65 | information on POP3 server variations and RFC noncompliance |
| 66 | that may be useful if you need to write an application based |
| 67 | on the POP protocol.} |
Fred Drake | a7c9ac6 | 2001-05-09 03:49:48 +0000 | [diff] [blame] | 68 | \end{seealso} |
| 69 | |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 70 | |
Fred Drake | e2effbd | 1999-04-22 16:21:09 +0000 | [diff] [blame] | 71 | \subsection{POP3 Objects \label{pop3-objects}} |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 72 | |
| 73 | All POP3 commands are represented by methods of the same name, |
Fred Drake | e2effbd | 1999-04-22 16:21:09 +0000 | [diff] [blame] | 74 | in lower-case; most return the response text sent by the server. |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 75 | |
| 76 | An \class{POP3} instance has the following methods: |
| 77 | |
| 78 | |
Georg Brandl | ae91afd | 2007-04-01 22:39:10 +0000 | [diff] [blame] | 79 | \begin{methoddesc}[POP3]{set_debuglevel}{level} |
Fred Drake | a16433b | 2001-12-05 22:37:21 +0000 | [diff] [blame] | 80 | Set the instance's debugging level. This controls the amount of |
| 81 | debugging output printed. The default, \code{0}, produces no |
| 82 | debugging output. A value of \code{1} produces a moderate amount of |
| 83 | debugging output, generally a single line per request. A value of |
| 84 | \code{2} or higher produces the maximum amount of debugging output, |
| 85 | logging each line sent and received on the control connection. |
| 86 | \end{methoddesc} |
| 87 | |
Georg Brandl | ae91afd | 2007-04-01 22:39:10 +0000 | [diff] [blame] | 88 | \begin{methoddesc}[POP3]{getwelcome}{} |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 89 | Returns the greeting string sent by the POP3 server. |
| 90 | \end{methoddesc} |
| 91 | |
Georg Brandl | ae91afd | 2007-04-01 22:39:10 +0000 | [diff] [blame] | 92 | \begin{methoddesc}[POP3]{user}{username} |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 93 | Send user command, response should indicate that a password is required. |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 94 | \end{methoddesc} |
| 95 | |
Georg Brandl | ae91afd | 2007-04-01 22:39:10 +0000 | [diff] [blame] | 96 | \begin{methoddesc}[POP3]{pass_}{password} |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 97 | Send password, response includes message count and mailbox size. |
| 98 | Note: the mailbox on the server is locked until \method{quit()} is |
| 99 | called. |
| 100 | \end{methoddesc} |
| 101 | |
Georg Brandl | ae91afd | 2007-04-01 22:39:10 +0000 | [diff] [blame] | 102 | \begin{methoddesc}[POP3]{apop}{user, secret} |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 103 | Use the more secure APOP authentication to log into the POP3 server. |
| 104 | \end{methoddesc} |
| 105 | |
Georg Brandl | ae91afd | 2007-04-01 22:39:10 +0000 | [diff] [blame] | 106 | \begin{methoddesc}[POP3]{rpop}{user} |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 107 | Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server. |
| 108 | \end{methoddesc} |
| 109 | |
Georg Brandl | ae91afd | 2007-04-01 22:39:10 +0000 | [diff] [blame] | 110 | \begin{methoddesc}[POP3]{stat}{} |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 111 | Get mailbox status. The result is a tuple of 2 integers: |
| 112 | \code{(\var{message count}, \var{mailbox size})}. |
| 113 | \end{methoddesc} |
| 114 | |
Georg Brandl | ae91afd | 2007-04-01 22:39:10 +0000 | [diff] [blame] | 115 | \begin{methoddesc}[POP3]{list}{\optional{which}} |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 116 | Request message list, result is in the form |
Georg Brandl | 2772c67 | 2005-08-05 21:01:58 +0000 | [diff] [blame] | 117 | \code{(\var{response}, ['mesg_num octets', ...], \var{octets})}. |
| 118 | If \var{which} is set, it is the message to list. |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 119 | \end{methoddesc} |
| 120 | |
Georg Brandl | ae91afd | 2007-04-01 22:39:10 +0000 | [diff] [blame] | 121 | \begin{methoddesc}[POP3]{retr}{which} |
Eric S. Raymond | 5ac9795 | 2001-01-11 04:19:52 +0000 | [diff] [blame] | 122 | Retrieve whole message number \var{which}, and set its seen flag. |
| 123 | Result is in form \code{(\var{response}, ['line', ...], \var{octets})}. |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 124 | \end{methoddesc} |
| 125 | |
Georg Brandl | ae91afd | 2007-04-01 22:39:10 +0000 | [diff] [blame] | 126 | \begin{methoddesc}[POP3]{dele}{which} |
Eric S. Raymond | 5ac9795 | 2001-01-11 04:19:52 +0000 | [diff] [blame] | 127 | Flag message number \var{which} for deletion. On most servers |
| 128 | deletions are not actually performed until QUIT (the major exception is |
Fred Drake | d8eeeae | 2002-10-18 16:50:17 +0000 | [diff] [blame] | 129 | Eudora QPOP, which deliberately violates the RFCs by doing pending |
Eric S. Raymond | 5ac9795 | 2001-01-11 04:19:52 +0000 | [diff] [blame] | 130 | deletes on any disconnect). |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 131 | \end{methoddesc} |
| 132 | |
Georg Brandl | ae91afd | 2007-04-01 22:39:10 +0000 | [diff] [blame] | 133 | \begin{methoddesc}[POP3]{rset}{} |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 134 | Remove any deletion marks for the mailbox. |
| 135 | \end{methoddesc} |
| 136 | |
Georg Brandl | ae91afd | 2007-04-01 22:39:10 +0000 | [diff] [blame] | 137 | \begin{methoddesc}[POP3]{noop}{} |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 138 | Do nothing. Might be used as a keep-alive. |
| 139 | \end{methoddesc} |
| 140 | |
Georg Brandl | ae91afd | 2007-04-01 22:39:10 +0000 | [diff] [blame] | 141 | \begin{methoddesc}[POP3]{quit}{} |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 142 | Signoff: commit changes, unlock mailbox, drop connection. |
| 143 | \end{methoddesc} |
| 144 | |
Georg Brandl | ae91afd | 2007-04-01 22:39:10 +0000 | [diff] [blame] | 145 | \begin{methoddesc}[POP3]{top}{which, howmuch} |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 146 | Retrieves the message header plus \var{howmuch} lines of the message |
Fred Drake | d8eeeae | 2002-10-18 16:50:17 +0000 | [diff] [blame] | 147 | after the header of message number \var{which}. Result is in form |
Fred Drake | 3a4ceb7 | 1999-07-07 14:04:38 +0000 | [diff] [blame] | 148 | \code{(\var{response}, ['line', ...], \var{octets})}. |
Eric S. Raymond | 5ac9795 | 2001-01-11 04:19:52 +0000 | [diff] [blame] | 149 | |
| 150 | The POP3 TOP command this method uses, unlike the RETR command, |
| 151 | doesn't set the message's seen flag; unfortunately, TOP is poorly |
| 152 | specified in the RFCs and is frequently broken in off-brand servers. |
| 153 | Test this method by hand against the POP3 servers you will use before |
| 154 | trusting it. |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 155 | \end{methoddesc} |
| 156 | |
Georg Brandl | ae91afd | 2007-04-01 22:39:10 +0000 | [diff] [blame] | 157 | \begin{methoddesc}[POP3]{uidl}{\optional{which}} |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 158 | Return message digest (unique id) list. |
Fred Drake | dbc2d08 | 1999-05-13 18:48:14 +0000 | [diff] [blame] | 159 | If \var{which} is specified, result contains the unique id for that |
| 160 | message in the form \code{'\var{response}\ \var{mesgnum}\ \var{uid}}, |
Fred Drake | 3a4ceb7 | 1999-07-07 14:04:38 +0000 | [diff] [blame] | 161 | otherwise result is list \code{(\var{response}, ['mesgnum uid', ...], |
| 162 | \var{octets})}. |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 163 | \end{methoddesc} |
| 164 | |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 165 | Instances of \class{POP3_SSL} have no additional methods. The |
| 166 | interface of this subclass is identical to its parent. |
| 167 | |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 168 | |
Fred Drake | e2effbd | 1999-04-22 16:21:09 +0000 | [diff] [blame] | 169 | \subsection{POP3 Example \label{pop3-example}} |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 170 | |
| 171 | Here is a minimal example (without error checking) that opens a |
| 172 | mailbox and retrieves and prints all messages: |
| 173 | |
| 174 | \begin{verbatim} |
Guido van Rossum | aac399b | 1998-12-08 16:30:10 +0000 | [diff] [blame] | 175 | import getpass, poplib |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 176 | |
| 177 | M = poplib.POP3('localhost') |
| 178 | M.user(getpass.getuser()) |
Guido van Rossum | aac399b | 1998-12-08 16:30:10 +0000 | [diff] [blame] | 179 | M.pass_(getpass.getpass()) |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 180 | numMessages = len(M.list()[1]) |
| 181 | for i in range(numMessages): |
| 182 | for j in M.retr(i+1)[1]: |
Guido van Rossum | aac399b | 1998-12-08 16:30:10 +0000 | [diff] [blame] | 183 | print j |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 184 | \end{verbatim} |
| 185 | |
| 186 | At the end of the module, there is a test section that contains a more |
| 187 | extensive example of usage. |