| 1 | \section{\module{poplib} ---
|
|---|
| 2 | POP3 protocol client}
|
|---|
| 3 |
|
|---|
| 4 | \declaremodule{standard}{poplib}
|
|---|
| 5 | \modulesynopsis{POP3 protocol client (requires sockets).}
|
|---|
| 6 |
|
|---|
| 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''.
|
|---|
| 11 | %Revised by ESR, January 2000
|
|---|
| 12 |
|
|---|
| 13 | \indexii{POP3}{protocol}
|
|---|
| 14 |
|
|---|
| 15 | This module defines a class, \class{POP3}, which encapsulates a
|
|---|
| 16 | connection to a POP3 server and implements the protocol as defined in
|
|---|
| 17 | \rfc{1725}. The \class{POP3} class supports both the minimal and
|
|---|
| 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 |
|
|---|
| 22 |
|
|---|
| 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
|
|---|
| 26 | using the \code{\refmodule{imaplib}.\class{IMAP4}} class, as IMAP
|
|---|
| 27 | servers tend to be better implemented.
|
|---|
| 28 |
|
|---|
| 29 | A single class is provided by the \module{poplib} module:
|
|---|
| 30 |
|
|---|
| 31 | \begin{classdesc}{POP3}{host\optional{, port}}
|
|---|
| 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.
|
|---|
| 35 | \end{classdesc}
|
|---|
| 36 |
|
|---|
| 37 | \begin{classdesc}{POP3_SSL}{host\optional{, port\optional{, keyfile\optional{, certfile}}}}
|
|---|
| 38 | This is a subclass of \class{POP3} that connects to the server over an
|
|---|
| 39 | SSL encrypted socket. If \var{port} is not specified, 995, the
|
|---|
| 40 | standard POP3-over-SSL port is used. \var{keyfile} and \var{certfile}
|
|---|
| 41 | are also optional - they can contain a PEM formatted private key and
|
|---|
| 42 | certificate chain file for the SSL connection.
|
|---|
| 43 |
|
|---|
| 44 | \versionadded{2.4}
|
|---|
| 45 | \end{classdesc}
|
|---|
| 46 |
|
|---|
| 47 | One exception is defined as an attribute of the \module{poplib} module:
|
|---|
| 48 |
|
|---|
| 49 | \begin{excdesc}{error_proto}
|
|---|
| 50 | Exception raised on any errors. The reason for the exception is
|
|---|
| 51 | passed to the constructor as a string.
|
|---|
| 52 | \end{excdesc}
|
|---|
| 53 |
|
|---|
| 54 | \begin{seealso}
|
|---|
| 55 | \seemodule{imaplib}{The standard Python IMAP module.}
|
|---|
| 56 | \seetitle[http://www.catb.org/\~{}esr/fetchmail/fetchmail-FAQ.html]
|
|---|
| 57 | {Frequently Asked Questions About Fetchmail}
|
|---|
| 58 | {The FAQ for the \program{fetchmail} POP/IMAP client collects
|
|---|
| 59 | information on POP3 server variations and RFC noncompliance
|
|---|
| 60 | that may be useful if you need to write an application based
|
|---|
| 61 | on the POP protocol.}
|
|---|
| 62 | \end{seealso}
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | \subsection{POP3 Objects \label{pop3-objects}}
|
|---|
| 66 |
|
|---|
| 67 | All POP3 commands are represented by methods of the same name,
|
|---|
| 68 | in lower-case; most return the response text sent by the server.
|
|---|
| 69 |
|
|---|
| 70 | An \class{POP3} instance has the following methods:
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 | \begin{methoddesc}{set_debuglevel}{level}
|
|---|
| 74 | Set the instance's debugging level. This controls the amount of
|
|---|
| 75 | debugging output printed. The default, \code{0}, produces no
|
|---|
| 76 | debugging output. A value of \code{1} produces a moderate amount of
|
|---|
| 77 | debugging output, generally a single line per request. A value of
|
|---|
| 78 | \code{2} or higher produces the maximum amount of debugging output,
|
|---|
| 79 | logging each line sent and received on the control connection.
|
|---|
| 80 | \end{methoddesc}
|
|---|
| 81 |
|
|---|
| 82 | \begin{methoddesc}{getwelcome}{}
|
|---|
| 83 | Returns the greeting string sent by the POP3 server.
|
|---|
| 84 | \end{methoddesc}
|
|---|
| 85 |
|
|---|
| 86 | \begin{methoddesc}{user}{username}
|
|---|
| 87 | Send user command, response should indicate that a password is required.
|
|---|
| 88 | \end{methoddesc}
|
|---|
| 89 |
|
|---|
| 90 | \begin{methoddesc}{pass_}{password}
|
|---|
| 91 | Send password, response includes message count and mailbox size.
|
|---|
| 92 | Note: the mailbox on the server is locked until \method{quit()} is
|
|---|
| 93 | called.
|
|---|
| 94 | \end{methoddesc}
|
|---|
| 95 |
|
|---|
| 96 | \begin{methoddesc}{apop}{user, secret}
|
|---|
| 97 | Use the more secure APOP authentication to log into the POP3 server.
|
|---|
| 98 | \end{methoddesc}
|
|---|
| 99 |
|
|---|
| 100 | \begin{methoddesc}{rpop}{user}
|
|---|
| 101 | Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server.
|
|---|
| 102 | \end{methoddesc}
|
|---|
| 103 |
|
|---|
| 104 | \begin{methoddesc}{stat}{}
|
|---|
| 105 | Get mailbox status. The result is a tuple of 2 integers:
|
|---|
| 106 | \code{(\var{message count}, \var{mailbox size})}.
|
|---|
| 107 | \end{methoddesc}
|
|---|
| 108 |
|
|---|
| 109 | \begin{methoddesc}{list}{\optional{which}}
|
|---|
| 110 | Request message list, result is in the form
|
|---|
| 111 | \code{(\var{response}, ['mesg_num octets', ...], \var{octets})}.
|
|---|
| 112 | If \var{which} is set, it is the message to list.
|
|---|
| 113 | \end{methoddesc}
|
|---|
| 114 |
|
|---|
| 115 | \begin{methoddesc}{retr}{which}
|
|---|
| 116 | Retrieve whole message number \var{which}, and set its seen flag.
|
|---|
| 117 | Result is in form \code{(\var{response}, ['line', ...], \var{octets})}.
|
|---|
| 118 | \end{methoddesc}
|
|---|
| 119 |
|
|---|
| 120 | \begin{methoddesc}{dele}{which}
|
|---|
| 121 | Flag message number \var{which} for deletion. On most servers
|
|---|
| 122 | deletions are not actually performed until QUIT (the major exception is
|
|---|
| 123 | Eudora QPOP, which deliberately violates the RFCs by doing pending
|
|---|
| 124 | deletes on any disconnect).
|
|---|
| 125 | \end{methoddesc}
|
|---|
| 126 |
|
|---|
| 127 | \begin{methoddesc}{rset}{}
|
|---|
| 128 | Remove any deletion marks for the mailbox.
|
|---|
| 129 | \end{methoddesc}
|
|---|
| 130 |
|
|---|
| 131 | \begin{methoddesc}{noop}{}
|
|---|
| 132 | Do nothing. Might be used as a keep-alive.
|
|---|
| 133 | \end{methoddesc}
|
|---|
| 134 |
|
|---|
| 135 | \begin{methoddesc}{quit}{}
|
|---|
| 136 | Signoff: commit changes, unlock mailbox, drop connection.
|
|---|
| 137 | \end{methoddesc}
|
|---|
| 138 |
|
|---|
| 139 | \begin{methoddesc}{top}{which, howmuch}
|
|---|
| 140 | Retrieves the message header plus \var{howmuch} lines of the message
|
|---|
| 141 | after the header of message number \var{which}. Result is in form
|
|---|
| 142 | \code{(\var{response}, ['line', ...], \var{octets})}.
|
|---|
| 143 |
|
|---|
| 144 | The POP3 TOP command this method uses, unlike the RETR command,
|
|---|
| 145 | doesn't set the message's seen flag; unfortunately, TOP is poorly
|
|---|
| 146 | specified in the RFCs and is frequently broken in off-brand servers.
|
|---|
| 147 | Test this method by hand against the POP3 servers you will use before
|
|---|
| 148 | trusting it.
|
|---|
| 149 | \end{methoddesc}
|
|---|
| 150 |
|
|---|
| 151 | \begin{methoddesc}{uidl}{\optional{which}}
|
|---|
| 152 | Return message digest (unique id) list.
|
|---|
| 153 | If \var{which} is specified, result contains the unique id for that
|
|---|
| 154 | message in the form \code{'\var{response}\ \var{mesgnum}\ \var{uid}},
|
|---|
| 155 | otherwise result is list \code{(\var{response}, ['mesgnum uid', ...],
|
|---|
| 156 | \var{octets})}.
|
|---|
| 157 | \end{methoddesc}
|
|---|
| 158 |
|
|---|
| 159 | Instances of \class{POP3_SSL} have no additional methods. The
|
|---|
| 160 | interface of this subclass is identical to its parent.
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 | \subsection{POP3 Example \label{pop3-example}}
|
|---|
| 164 |
|
|---|
| 165 | Here is a minimal example (without error checking) that opens a
|
|---|
| 166 | mailbox and retrieves and prints all messages:
|
|---|
| 167 |
|
|---|
| 168 | \begin{verbatim}
|
|---|
| 169 | import getpass, poplib
|
|---|
| 170 |
|
|---|
| 171 | M = poplib.POP3('localhost')
|
|---|
| 172 | M.user(getpass.getuser())
|
|---|
| 173 | M.pass_(getpass.getpass())
|
|---|
| 174 | numMessages = len(M.list()[1])
|
|---|
| 175 | for i in range(numMessages):
|
|---|
| 176 | for j in M.retr(i+1)[1]:
|
|---|
| 177 | print j
|
|---|
| 178 | \end{verbatim}
|
|---|
| 179 |
|
|---|
| 180 | At the end of the module, there is a test section that contains a more
|
|---|
| 181 | extensive example of usage.
|
|---|