1 \section{\module{poplib
} ---
4 \declaremodule{standard
}{poplib
}
5 \modulesynopsis{POP3 protocol client (requires sockets).
}
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
13 \indexii{POP3
}{protocol
}
15 This module defines a class,
\class{POP3
}, which encapsulates a
16 connection to an 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.
20 Note that POP3, though widely supported, is obsolescent. The
21 implementation quality of POP3 servers varies widely, and too many are
22 quite poor. If your mailserver supports IMAP, you would be better off
23 using the
\code{\refmodule{imaplib
}.
\class{IMAP4
}} class, as IMAP
24 servers tend to be better implemented.
26 A single class is provided by the
\module{poplib
} module:
28 \begin{classdesc
}{POP3
}{host
\optional{, port
}}
29 This class implements the actual POP3 protocol. The connection is
30 created when the instance is initialized.
31 If
\var{port
} is omitted, the standard POP3 port (
110) is used.
34 One exception is defined as an attribute of the
\module{poplib
} module:
36 \begin{excdesc
}{error_proto
}
37 Exception raised on any errors. The reason for the exception is
38 passed to the constructor as a string.
42 \seemodule{imaplib
}{The standard Python IMAP module.
}
43 \seetitle[http://www.tuxedo.org/\~
{}esr/fetchmail/fetchmail-FAQ.html
]
44 {Frequently Asked Questions About Fetchmail
}
45 {The FAQ for the
\program{fetchmail
} POP/IMAP client collects
46 information on POP3 server variations and RFC noncompliance
47 that may be useful if you need to write an application based
52 \subsection{POP3 Objects
\label{pop3-objects
}}
54 All POP3 commands are represented by methods of the same name,
55 in lower-case; most return the response text sent by the server.
57 An
\class{POP3
} instance has the following methods:
60 \begin{methoddesc
}{set_debuglevel
}{level
}
61 Set the instance's debugging level. This controls the amount of
62 debugging output printed. The default,
\code{0}, produces no
63 debugging output. A value of
\code{1} produces a moderate amount of
64 debugging output, generally a single line per request. A value of
65 \code{2} or higher produces the maximum amount of debugging output,
66 logging each line sent and received on the control connection.
69 \begin{methoddesc
}{getwelcome
}{}
70 Returns the greeting string sent by the POP3 server.
73 \begin{methoddesc
}{user
}{username
}
74 Send user command, response should indicate that a password is required.
77 \begin{methoddesc
}{pass_
}{password
}
78 Send password, response includes message count and mailbox size.
79 Note: the mailbox on the server is locked until
\method{quit()
} is
83 \begin{methoddesc
}{apop
}{user, secret
}
84 Use the more secure APOP authentication to log into the POP3 server.
87 \begin{methoddesc
}{rpop
}{user
}
88 Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server.
91 \begin{methoddesc
}{stat
}{}
92 Get mailbox status. The result is a tuple of
2 integers:
93 \code{(
\var{message count
},
\var{mailbox size
})
}.
96 \begin{methoddesc
}{list
}{\optional{which
}}
97 Request message list, result is in the form
98 \code{(
\var{response
},
['mesg_num octets', ...
])
}. If
\var{which
} is
99 set, it is the message to list.
102 \begin{methoddesc
}{retr
}{which
}
103 Retrieve whole message number
\var{which
}, and set its seen flag.
104 Result is in form
\code{(
\var{response
},
['line', ...
],
\var{octets
})
}.
107 \begin{methoddesc
}{dele
}{which
}
108 Flag message number
\var{which
} for deletion. On most servers
109 deletions are not actually performed until QUIT (the major exception is
110 Eudora QPOP, which deliberately violates the RFCs by doing pending
111 deletes on any disconnect).
114 \begin{methoddesc
}{rset
}{}
115 Remove any deletion marks for the mailbox.
118 \begin{methoddesc
}{noop
}{}
119 Do nothing. Might be used as a keep-alive.
122 \begin{methoddesc
}{quit
}{}
123 Signoff: commit changes, unlock mailbox, drop connection.
126 \begin{methoddesc
}{top
}{which, howmuch
}
127 Retrieves the message header plus
\var{howmuch
} lines of the message
128 after the header of message number
\var{which
}. Result is in form
129 \code{(
\var{response
},
['line', ...
],
\var{octets
})
}.
131 The POP3 TOP command this method uses, unlike the RETR command,
132 doesn't set the message's seen flag; unfortunately, TOP is poorly
133 specified in the RFCs and is frequently broken in off-brand servers.
134 Test this method by hand against the POP3 servers you will use before
138 \begin{methoddesc
}{uidl
}{\optional{which
}}
139 Return message digest (unique id) list.
140 If
\var{which
} is specified, result contains the unique id for that
141 message in the form
\code{'
\var{response
}\
\var{mesgnum
}\
\var{uid
}},
142 otherwise result is list
\code{(
\var{response
},
['mesgnum uid', ...
],
147 \subsection{POP3 Example
\label{pop3-example
}}
149 Here is a minimal example (without error checking) that opens a
150 mailbox and retrieves and prints all messages:
153 import getpass, poplib
155 M = poplib.POP3('localhost')
156 M.user(getpass.getuser())
157 M.pass_(getpass.getpass())
158 numMessages = len(M.list()
[1])
159 for i in range(numMessages):
160 for j in M.retr(i+
1)
[1]:
164 At the end of the module, there is a test section that contains a more
165 extensive example of usage.