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