poplib — POP3 protocol client

Source code:Lib/poplib.py


This module defines a class,POP3, which encapsulates a connection to aPOP3 server and implements the protocol as defined inRFC 1939. ThePOP3 class supports both the minimal and optional command sets fromRFC 1939. ThePOP3 class also supports theSTLS command introducedinRFC 2595 to enable encrypted communication on an already established connection.

Additionally, this module provides a classPOP3_SSL, which providessupport for connecting to POP3 servers that use SSL as an underlying protocollayer.

Note that POP3, though widely supported, is obsolescent. The implementationquality of POP3 servers varies widely, and too many are quite poor. If yourmailserver supports IMAP, you would be better off using theimaplib.IMAP4 class, as IMAP servers tend to be better implemented.

Διαθεσιμότητα: not WASI.

This module does not work or is not available on WebAssembly. SeeWebAssembly platforms for more information.

Thepoplib module provides two classes:

classpoplib.POP3(host,port=POP3_PORT[,timeout])

This class implements the actual POP3 protocol. The connection is created whenthe instance is initialized. Ifport is omitted, the standard POP3 port (110)is used. The optionaltimeout parameter specifies a timeout in seconds for theconnection attempt (if not specified, the global default timeout setting willbe used).

Raises anauditing eventpoplib.connect with argumentsself,host,port.

All commands will raise anauditing eventpoplib.putline with argumentsself andline,whereline is the bytes about to be sent to the remote host.

Άλλαξε στην έκδοση 3.9:If thetimeout parameter is set to be zero, it will raise aValueError to prevent the creation of a non-blocking socket.

classpoplib.POP3_SSL(host,port=POP3_SSL_PORT,*,timeout=None,context=None)

This is a subclass ofPOP3 that connects to the server over an SSLencrypted socket. Ifport is not specified, 995, the standard POP3-over-SSLport is used.timeout works as in thePOP3 constructor.context is an optionalssl.SSLContext object which allowsbundling SSL configuration options, certificates and private keys into asingle (potentially long-lived) structure. Please readSecurity considerationsfor best practices.

Raises anauditing eventpoplib.connect with argumentsself,host,port.

All commands will raise anauditing eventpoplib.putline with argumentsself andline,whereline is the bytes about to be sent to the remote host.

Άλλαξε στην έκδοση 3.2:context parameter added.

Άλλαξε στην έκδοση 3.4:The class now supports hostname check withssl.SSLContext.check_hostname andServer Name Indication (seessl.HAS_SNI).

Άλλαξε στην έκδοση 3.9:If thetimeout parameter is set to be zero, it will raise aValueError to prevent the creation of a non-blocking socket.

Άλλαξε στην έκδοση 3.12:The deprecatedkeyfile andcertfile parameters have been removed.

One exception is defined as an attribute of thepoplib module:

exceptionpoplib.error_proto

Exception raised on any errors from this module (errors fromsocketmodule are not caught). The reason for the exception is passed to theconstructor as a string.

Δείτε επίσης

Moduleimaplib

The standard Python IMAP module.

Frequently Asked Questions About Fetchmail

The FAQ for thefetchmail POP/IMAP client collects information onPOP3 server variations and RFC noncompliance that may be useful if you need towrite an application based on the POP protocol.

POP3 Objects

All POP3 commands are represented by methods of the same name, in lowercase;most return the response text sent by the server.

APOP3 instance has the following methods:

POP3.set_debuglevel(level)

Set the instance’s debugging level. This controls the amount of debuggingoutput printed. The default,0, produces no debugging output. A value of1 produces a moderate amount of debugging output, generally a single lineper request. A value of2 or higher produces the maximum amount ofdebugging output, logging each line sent and received on the control connection.

POP3.getwelcome()

Returns the greeting string sent by the POP3 server.

POP3.capa()

Query the server’s capabilities as specified inRFC 2449.Returns a dictionary in the form{'name':['param'...]}.

Added in version 3.4.

POP3.user(username)

Send user command, response should indicate that a password is required.

POP3.pass_(password)

Send password, response includes message count and mailbox size. Note: themailbox on the server is locked untilquit() is called.

POP3.apop(user,secret)

Use the more secure APOP authentication to log into the POP3 server.

POP3.rpop(user)

Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server.

POP3.stat()

Get mailbox status. The result is a tuple of 2 integers:(messagecount,mailboxsize).

POP3.list([which])

Request message list, result is in the form(response,['mesg_numoctets',...],octets). Ifwhich is set, it is the message to list.

POP3.retr(which)

Retrieve whole message numberwhich, and set its seen flag. Result is in form(response,['line',...],octets).

POP3.dele(which)

Flag message numberwhich for deletion. On most servers deletions are notactually performed until QUIT (the major exception is Eudora QPOP, whichdeliberately violates the RFCs by doing pending deletes on any disconnect).

POP3.rset()

Remove any deletion marks for the mailbox.

POP3.noop()

Do nothing. Might be used as a keep-alive.

POP3.quit()

Signoff: commit changes, unlock mailbox, drop connection.

POP3.top(which,howmuch)

Retrieves the message header plushowmuch lines of the message after theheader of message numberwhich. Result is in form(response,['line',...],octets).

The POP3 TOP command this method uses, unlike the RETR command, doesn’t set themessage’s seen flag; unfortunately, TOP is poorly specified in the RFCs and isfrequently broken in off-brand servers. Test this method by hand against thePOP3 servers you will use before trusting it.

POP3.uidl(which=None)

Return message digest (unique id) list. Ifwhich is specified, result containsthe unique id for that message in the form'responsemesgnumuid, otherwiseresult is list(response,['mesgnumuid',...],octets).

POP3.utf8()

Try to switch to UTF-8 mode. Returns the server response if successful,raiseserror_proto if not. Specified inRFC 6856.

Added in version 3.5.

POP3.stls(context=None)

Start a TLS session on the active connection as specified inRFC 2595.This is only allowed before user authentication

context parameter is assl.SSLContext object which allowsbundling SSL configuration options, certificates and private keys intoa single (potentially long-lived) structure. Please readSecurity considerationsfor best practices.

This method supports hostname checking viassl.SSLContext.check_hostname andServer Name Indication (seessl.HAS_SNI).

Added in version 3.4.

Instances ofPOP3_SSL have no additional methods. The interface of thissubclass is identical to its parent.

POP3 Example

Here is a minimal example (without error checking) that opens a mailbox andretrieves and prints all messages:

importgetpass,poplibM=poplib.POP3('localhost')M.user(getpass.getuser())M.pass_(getpass.getpass())numMessages=len(M.list()[1])foriinrange(numMessages):forjinM.retr(i+1)[1]:print(j)

At the end of the module, there is a test section that contains a more extensiveexample of usage.