class twisted.mail.pop3.AdvancedPOP3Client(basic.LineOnlyReceiver, policies.TimeoutMixin): (source)
A POP3 client protocol.
Instances of this class provide a convenient, efficient API for retrieving and deleting messages from a POP3 server.
This API provides a pipelining interface but POP3 pipelining on the network is not yet supported.
Instance Variable | startedTLS | An indication of whether TLS has been negotiated successfully. |
Instance Variable | allowInsecureLogin | An indication of whether plaintext login should be allowed when the server offers no authentication challenge and the transport does not offer any protection via encryption. |
Instance Variable | serverChallenge | The challenge received in the server greeting. |
Instance Variable | timeout | The number of seconds to wait on a response from the server before timing out a connection. If the number is <= 0, no timeout checking will be performed. |
Instance Variable | state | The state which indicates what type of response is expected from the server. Valid states are: 'WELCOME', 'WAITING', 'SHORT', 'LONG_INITIAL', 'LONG'. |
Method | sendShort | Send a POP3 command to which a short response is expected. |
Method | sendLong | Send a POP3 command to which a multi-line response is expected. |
Method | connectionMade | Wait for a greeting from the server after the connection has been made. |
Method | timeoutConnection | Drop the connection when the server does not respond in time. |
Method | connectionLost | Clean up when the connection has been lost. |
Method | lineReceived | Pass a received line to a state machine function and transition to the next state. |
Method | lineLengthExceeded | Drop the connection when a server response exceeds the maximum line length (LineOnlyReceiver.MAX_LENGTH ). |
Method | state_WELCOME | Handle server responses for the WELCOME state in which the server greeting is expected. |
Method | state_WAITING | Log an error for server responses received in the WAITING state during which the server is not expected to send anything. |
Method | state_SHORT | Handle server responses for the SHORT state in which the server is expected to send a single line response. |
Method | state_LONG_INITIAL | Handle server responses for the LONG_INITIAL state in which the server is expected to send the first line of a multi-line response. |
Method | state_LONG | Handle server responses for the LONG state in which the server is expected to send a non-initial line of a multi-line response. |
Method | serverGreeting | Handle the server greeting. |
Method | startTLS | Switch to encrypted communication using TLS. |
Instance Variable | transport | Undocumented |
Method | login | Log in to the server. |
Method | apop | Send an APOP command to perform authenticated login. |
Method | user | Send a USER command to perform the first half of plaintext login. |
Method | password | Send a PASS command to perform the second half of plaintext login. |
Method | delete | Send a DELE command to delete a message from the server. |
Method | capabilities | Send a CAPA command to retrieve the capabilities supported by the server. |
Method | noop | Send a NOOP command asking the server to do nothing but respond. |
Method | reset | Send a RSET command to unmark any messages that have been flagged for deletion on the server. |
Method | retrieve | Send a RETR or TOP command to retrieve all or part of a message from the server. |
Method | stat | Send a STAT command to get information about the size of the mailbox. |
Method | listSize | Send a LIST command to retrieve the sizes of all messages on the server. |
Method | listUID | Send a UIDL command to retrieve the UIDs of all messages on the server. |
Method | quit | Send a QUIT command to disconnect from the server. |
Instance Variable | _capCache | The cached server capabilities. Capabilities are not allowed to change during the session (except when TLS is negotiated), so the first response to a capabilities command can be used for later lookups. |
Instance Variable | _challengeMagicRe | A regular expression which matches the challenge in the server greeting. |
Instance Variable | _blockedQueue | A list of blocked commands. While a command is awaiting a response from the server, other commands are blocked. When no command is outstanding, _blockedQueue is set to None . Otherwise, it contains a list of information about blocked commands. Each list entry provides the following information about a blocked command: the deferred that should be called when the response to the command is received, the function that sends the command, and the arguments to the function. |
Instance Variable | _waiting | A deferred which fires when the response to the outstanding command is received from the server. |
Instance Variable | _timedOut | An indication of whether the connection was dropped because of a timeout. |
Instance Variable | _greetingError | The server greeting minus the status indicator, when the connection was dropped because of an error in the server greeting. Otherwise, None . |
Instance Variable | _xform | The transform function which is used to convert each line of a multi-line response into usable values for use by the consumer function. If None , each line of the multi-line response is sent directly to the consumer function. |
Instance Variable | _consumer | The consumer function which is used to store the values derived by the transform function from each line of a multi-line response into a list. |
Method | _blocked | Block a command, if necessary. |
Method | _unblock | Send the next blocked command. |
Method | _startTLS | Continue the process of switching to encrypted communication. |
Method | _startedTLS | Complete the process of switching to encrypted communication. |
Method | _getContextFactory | Get a context factory with which to negotiate TLS. |
Method | _login | Continue the process of logging in to the server. |
Method | _loginTLS | Do a plaintext login over an encrypted transport. |
Method | _plaintext | Perform a plaintext login. |
Method | _apop | Perform an APOP login. |
Method | _consumeOrSetItem | Send a command to which a long response is expected and process the multi-line response into a list accounting for deleted messages. |
Method | _consumeOrAppend | Send a command to which a long response is expected and process the multi-line response into a list. |
Inherited from LineOnlyReceiver:
Class Variable | delimiter | The line-ending delimiter to use. By default this is b'\r\n' . |
Class Variable | MAX_LENGTH | The maximum length of a line to allow (If a sent line is longer than this, the connection is dropped). Default is 16384. |
Method | dataReceived | Translates bytes into lines, and calls lineReceived. |
Method | sendLine | Sends a line to the other end of the connection. |
Instance Variable | _buffer | Undocumented |
Inherited from Protocol (via LineOnlyReceiver):
Class Variable | factory | Undocumented |
Method | logPrefix | Return a prefix matching the class name, to identify log messages related to this protocol instance. |
Inherited from BaseProtocol (via LineOnlyReceiver, Protocol):
Instance Variable | connected | Undocumented |
Method | makeConnection | Make a connection to a transport and a server. |
Inherited from TimeoutMixin:
Instance Variable | timeOut | The number of seconds after which to timeout the connection. |
Method | callLater | Wrapper around reactor.callLater for test purpose. |
Method | resetTimeout | Reset the timeout count down. |
Method | setTimeout | Change the timeout period |
Instance Variable | __timeoutCall | Undocumented |
Method | __timedOut | Undocumented |
bool
)
int
)
Pattern
)
_blockedQueue
is set to None
. Otherwise, it contains a list of information about blocked commands. Each list entry provides the following information about a blocked command: the deferred that should be called when the response to the command is received, the function that sends the command, and the arguments to the function.None
or list
of 3-tuple
of (0) Deferred
, (1) callable which results in a Deferred
, (2) tuple
)
bool
)
bytes
)
object
)
Block a command, if necessary.
If commands are being blocked, append information about the function which sends the command to a list and return a deferred that will be chained with the return value of the function when it eventually runs. Otherwise, set up for subsequent commands to be blocked and return None
.
Parameters | f | A function which sends a command. (type: callable) |
a | Arguments to the function. (type: tuple ) | |
Returns | None if the command can run immediately. Otherwise, a deferred that will eventually trigger with the return value of the function. (type: None or Deferred ) |
Send the next blocked command.
If there are no more commands in the blocked queue, set up for the next command to be sent immediately.
Send a POP3 command to which a short response is expected.
Block all further commands from being sent until the response is received. Transition the state to SHORT.
Parameters | cmd | A POP3 command. (type: bytes ) |
args | The command arguments. (type: bytes ) | |
Returns | A deferred which fires when the entire response is received. On an OK response, it returns the response from the server minus the status indicator. On an ERR response, it issues a server error response failure with the response from the server minus the status indicator. (type: Deferred which successfully fires with bytes or fails with ServerErrorResponse ) |
Send a POP3 command to which a multi-line response is expected.
Block all further commands from being sent until the entire response is received. Transition the state to LONG_INITIAL.
Parameters | cmd | A POP3 command. (type: bytes ) |
args | The command arguments. (type: bytes ) | |
consumer | A consumer function which should be used to put the values derived by a transform function from each line of the multi-line response into a list. (type: callable that takes object ) | |
xform | A transform function which should be used to transform each line of the multi-line response into usable values for use by a consumer function. If None , each line of the multi-line response should be sent directly to the consumer function. (type: None or callable that takes bytes and returns object ) | |
Returns | A deferred which fires when the entire response is received. On an OK response, it returns the consumer function. On an ERR response, it issues a server error response failure with the response from the server minus the status indicator and the consumer function. (type: Deferred which successfully fires with callable that takes object and fails with ServerErrorResponse ) |
Wait for a greeting from the server after the connection has been made.
Start the connection in the WELCOME state.
Clean up when the connection has been lost.
When the loss of connection was initiated by the client due to a timeout, the _timedOut
flag will be set. When it was initiated by the client due to an error in the server greeting, _greetingError
will be set to the server response minus the status indicator.
Parameters | reason | The reason the connection was terminated. (type: Failure ) |
Pass a received line to a state machine function and transition to the next state.
Parameters | line | A received line. (type: bytes ) |
Drop the connection when a server response exceeds the maximum line length (LineOnlyReceiver.MAX_LENGTH
).
Parameters | buffer | A received line which exceeds the maximum line length. (type: bytes ) |
Handle server responses for the WELCOME state in which the server greeting is expected.
WELCOME is the first state. The server should send one line of text with a greeting and possibly an APOP challenge. Transition the state to WAITING.
Parameters | line | A line received from the server. (type: bytes ) |
Returns | The next state. (type: bytes ) |
Log an error for server responses received in the WAITING state during which the server is not expected to send anything.
Parameters | line | A line received from the server. (type: bytes ) |
Handle server responses for the SHORT state in which the server is expected to send a single line response.
Parse the response and fire the deferred which is waiting on receipt of a complete response. Transition the state back to WAITING.
Parameters | line | A line received from the server. (type: bytes ) |
Returns | The next state. (type: bytes ) |
Handle server responses for the LONG_INITIAL state in which the server is expected to send the first line of a multi-line response.
Parse the response. On an OK response, transition the state to LONG. On an ERR response, cleanup and transition the state to WAITING.
Parameters | line | A line received from the server. (type: bytes ) |
Returns | The next state. (type: bytes ) |
Handle server responses for the LONG state in which the server is expected to send a non-initial line of a multi-line response.
On receipt of the last line of the response, clean up, fire the deferred which is waiting on receipt of a complete response, and transition the state to WAITING. Otherwise, pass the line to the transform function, if provided, and then the consumer function.
Parameters | line | A line received from the server. (type: bytes ) |
Returns | The next state. (type: bytes ) |
Handle the server greeting.
Parameters | greeting | The server greeting minus the status indicator. For servers implementing APOP authentication, this will contain a challenge string. (type: bytes ) |
Switch to encrypted communication using TLS.
The first step of switching to encrypted communication is obtaining the server's capabilities. When that is complete, the _startTLS
callback function continues the switching process.
Parameters | contextFactory | The context factory with which to negotiate TLS. If not provided, try to create a new one. (type: None or ClientContextFactory ) |
Returns | A deferred which fires when the transport has been secured according to the given context factory with the server capabilities, or which fails with a TLS error if the transport cannot be secured. (type: Deferred which successfully results in dict mapping bytes to list of bytes and/or bytes to None or fails with TLSError ) |
Continue the process of switching to encrypted communication.
This callback function runs after the server capabilities are received.
The next step is sending the server an STLS command to request a switch to encrypted communication. When an OK response is received, the _startedTLS
callback function completes the switch to encrypted communication. Then, the new server capabilities are requested.
Parameters | caps | The server capabilities. (type: dict mapping bytes to list of bytes and/or bytes to None ) |
contextFactory | A context factory with which to negotiate TLS. (type: ClientContextFactory ) | |
tls | A TCP transport that supports switching to TLS midstream. (type: ITLSTransport ) | |
Returns | A deferred which successfully fires when the response from the server to the request to start TLS has been received and the new server capabilities have been received or fails when the server does not support TLS. (type: Deferred which successfully triggers with dict mapping bytes to list of bytes and/or bytes to None or fails with TLSNotSupportedError ) |
Complete the process of switching to encrypted communication.
This callback function runs after the response to the STLS command has been received.
The final steps are discarding the cached capabilities and initiating TLS negotiation on the transport.
Parameters | result | The server capabilities. (type: dict mapping bytes to list of bytes and/or bytes to None ) |
context | A context factory with which to negotiate TLS. (type: ClientContextFactory ) | |
tls | A TCP transport that supports switching to TLS midstream. (type: ITLSTransport ) | |
Returns | The server capabilities. (type: dict mapping bytes to list of bytes and/or bytes to None ) |
Get a context factory with which to negotiate TLS.
Returns | A context factory or None if TLS is not supported on the client. (type: None or ClientContextFactory ) |
Log in to the server.
If APOP is available it will be used. Otherwise, if TLS is available, an encrypted session will be started and plaintext login will proceed. Otherwise, if allowInsecureLogin
is set, insecure plaintext login will proceed. Otherwise, InsecureAuthenticationDisallowed
will be raised.
The first step of logging into the server is obtaining the server's capabilities. When that is complete, the _login
callback function continues the login process.
Parameters | username | The username with which to log in. (type: bytes ) |
password | The password with which to log in. (type: bytes ) | |
Returns | A deferred which fires when the login process is complete. On a successful login, it returns the server's response minus the status indicator. (type: Deferred which successfully fires with bytes ) |
Continue the process of logging in to the server.
This callback function runs after the server capabilities are received.
If the server provided a challenge in the greeting, proceed with an APOP login. Otherwise, if the server and the transport support encrypted communication, try to switch to TLS and then complete the login process with the _loginTLS
callback function. Otherwise, if insecure authentication is allowed, do a plaintext login. Otherwise, fail with an InsecureAuthenticationDisallowed
error.
Parameters | caps | The server capabilities. (type: dict mapping bytes to list of bytes and/or bytes to None ) |
username | The username with which to log in. (type: bytes ) | |
password | The password with which to log in. (type: bytes ) | |
Returns | A deferred which fires when the login process is complete. On a successful login, it returns the server's response minus the status indicator. (type: Deferred which successfully fires with bytes ) |
Do a plaintext login over an encrypted transport.
This callback function runs after the transport switches to encrypted communication.
Parameters | res | The server capabilities. (type: dict mapping bytes to list of bytes and/or bytes to None ) |
username | The username with which to log in. (type: bytes ) | |
password | The password with which to log in. (type: bytes ) | |
Returns | A deferred which fires when the server accepts the username and password or fails when the server rejects either. On a successful login, it returns the server's response minus the status indicator. (type: Deferred which successfully fires with bytes or fails with ServerErrorResponse ) |
Perform a plaintext login.
Parameters | username | The username with which to log in. (type: bytes ) |
password | The password with which to log in. (type: bytes ) | |
Returns | A deferred which fires when the server accepts the username and password or fails when the server rejects either. On a successful login, it returns the server's response minus the status indicator. (type: Deferred which successfully fires with bytes or fails with ServerErrorResponse ) |
Perform an APOP login.
Parameters | username | The username with which to log in. (type: bytes ) |
password | The password with which to log in. (type: bytes ) | |
challenge | A challenge string. (type: bytes ) | |
Returns | A deferred which fires when the server response is received. On a successful login, it returns the server response minus the status indicator. (type: Deferred which successfully fires with bytes or fails with ServerErrorResponse ) |
Send an APOP command to perform authenticated login.
This should be used in special circumstances only, when it is known that the server supports APOP authentication, and APOP authentication is absolutely required. For the common case, use login
instead.
Parameters | username | The username with which to log in. (type: bytes ) |
digest | The challenge response to authenticate with. (type: bytes ) | |
Returns | A deferred which fires when the server response is received. On an OK response, the deferred succeeds with the server response minus the status indicator. On an ERR response, the deferred fails with a server error response failure. (type: Deferred which successfully fires with bytes or fails with ServerErrorResponse ) |
Send a USER command to perform the first half of plaintext login.
Unless this is absolutely required, use the login
method instead.
Parameters | username | The username with which to log in. (type: bytes ) |
Returns | A deferred which fires when the server response is received. On an OK response, the deferred succeeds with the server response minus the status indicator. On an ERR response, the deferred fails with a server error response failure. (type: Deferred which successfully fires with bytes or fails with ServerErrorResponse ) |
Send a PASS command to perform the second half of plaintext login.
Unless this is absolutely required, use the login
method instead.
Parameters | password | The plaintext password with which to authenticate. (type: bytes ) |
Returns | A deferred which fires when the server response is received. On an OK response, the deferred succeeds with the server response minus the status indicator. On an ERR response, the deferred fails with a server error response failure. (type: Deferred which successfully fires with bytes or fails with ServerErrorResponse ) |
Send a DELE command to delete a message from the server.
Parameters | index | The 0-based index of the message to delete. (type: int ) |
Returns | A deferred which fires when the server response is received. On an OK response, the deferred succeeds with the server response minus the status indicator. On an ERR response, the deferred fails with a server error response failure. (type: Deferred which successfully fires with bytes or fails with ServerErrorResponse ) |
Send a command to which a long response is expected and process the multi-line response into a list accounting for deleted messages.
Parameters | cmd | A POP3 command to which a long response is expected. (type: bytes ) |
args | The command arguments. (type: bytes ) | |
consumer | None or a function that consumes the output from the transform function. (type: None or callable that takes object ) | |
xform | A function that parses a line from a multi-line response and transforms the values into usable form for input to the consumer function. If no consumer function is specified, the output must be a message index and corresponding value. If no transform function is specified, the line is used as is. (type: None , callable that takes bytes and returns 2-tuple of (0) int , (1) object , or callable that takes bytes and returns object ) | |
Returns | A deferred which fires when the entire response has been received. When a consumer is not provided, the return value is a list of the value for each message or None for deleted messages. Otherwise, it returns the consumer itself. (type: Deferred which fires with list of object or callable that takes list of object ) |
Send a command to which a long response is expected and process the multi-line response into a list.
Parameters | cmd | A POP3 command which expects a long response. (type: bytes ) |
args | The command arguments. (type: bytes ) | |
consumer | None or a function that consumes the output from the transform function. (type: None or callable that takes object ) | |
xform | A function that transforms a line from a multi-line response into usable form for input to the consumer function. If no transform function is specified, the line is used as is. (type: None or callable that takes bytes and returns object ) | |
Returns | A deferred which fires when the entire response has been received. When a consumer is not provided, the return value is a list of the transformed lines. Otherwise, it returns the consumer itself. (type: Deferred which fires with list of 2-tuple of (0) int , (1) object or callable that takes 2-tuple of (0) int , (1) object ) |
Send a CAPA command to retrieve the capabilities supported by the server.
Not all servers support this command. If the server does not support this, it is treated as though it returned a successful response listing no capabilities. At some future time, this may be changed to instead seek out information about a server's capabilities in some other fashion (only if it proves useful to do so, and only if there are servers still in use which do not support CAPA but which do support POP3 extensions that are useful).
Parameters | useCache | A flag that determines whether previously retrieved results should be used if available. (type: bool ) |
Returns | A deferred which fires with a mapping of capability name to parameters. For example:C: CAPA S: +OK Capability list follows S: TOP S: USER S: SASL CRAM-MD5 KERBEROS_V4 S: RESP-CODES S: LOGIN-DELAY 900 S: PIPELINING S: EXPIRE 60 S: UIDL S: IMPLEMENTATION Shlemazle-Plotz-v302 S: . will be lead to a result of: | {'TOP': None, | 'USER': None, | 'SASL': ['CRAM-MD5', 'KERBEROS_V4'], | 'RESP-CODES': None, | 'LOGIN-DELAY': ['900'], | 'PIPELINING': None, | 'EXPIRE': ['60'], | 'UIDL': None, | 'IMPLEMENTATION': ['Shlemazle-Plotz-v302']}(type: Deferred which successfully results in dict mapping bytes to list of bytes and/or bytes to None ) |
Send a NOOP command asking the server to do nothing but respond.
Returns | A deferred which fires when the server response is received. On an OK response, the deferred succeeds with the server response minus the status indicator. On an ERR response, the deferred fails with a server error response failure. (type: Deferred which successfully fires with bytes or fails with ServerErrorResponse ) |
Send a RSET command to unmark any messages that have been flagged for deletion on the server.
Returns | A deferred which fires when the server response is received. On an OK response, the deferred succeeds with the server response minus the status indicator. On an ERR response, the deferred fails with a server error response failure. (type: Deferred which successfully fires with bytes or fails with ServerErrorResponse ) |
Send a RETR or TOP command to retrieve all or part of a message from the server.
Parameters | index | A 0-based message index. (type: int ) |
consumer | A function which consumes each transformed line from a multi-line response as it is received. (type: None or callable that takes bytes ) | |
lines | If specified, the number of lines of the message to be retrieved. Otherwise, the entire message is retrieved. (type: None or int ) | |
Returns | A deferred which fires when the entire response has been received. When a consumer is not provided, the return value is a list of the transformed lines. Otherwise, it returns the consumer itself. (type: Deferred which fires with list of bytes , or callable that takes 2-tuple of (0) int , (1) object ) |
Send a STAT command to get information about the size of the mailbox.
Returns | A deferred which fires when the server response is received. On an OK response, the deferred succeeds with the number of messages in the mailbox and the size of the mailbox in octets. On an ERR response, the deferred fails with a server error response failure. (type: Deferred which successfully fires with a 2-tuple of (0) int , (1) int or fails with ServerErrorResponse ) |
Send a LIST command to retrieve the sizes of all messages on the server.
Parameters | consumer | A function which consumes the 0-based message index and message size derived from the server response. (type: None or callable that takes 2-tuple of (0) int , (1) int ) |
Returns | A deferred which fires when the entire response has been received. When a consumer is not provided, the return value is a list of message sizes. Otherwise, it returns the consumer itself. (type: Deferred which fires list of int or callable that takes 2-tuple of (0) int , (1) int ) |
Send a UIDL command to retrieve the UIDs of all messages on the server.
Parameters | consumer | A function which consumes the 0-based message index and UID derived from the server response. (type: None or callable that takes 2-tuple of (0) int , (1) bytes ) |
Returns | A deferred which fires when the entire response has been received. When a consumer is not provided, the return value is a list of message sizes. Otherwise, it returns the consumer itself. (type: Deferred which fires with list of object or callable that takes 2-tuple of (0) int , (1) bytes ) |
Send a QUIT command to disconnect from the server.
Returns | A deferred which fires when the server response is received. On an OK response, the deferred succeeds with the server response minus the status indicator. On an ERR response, the deferred fails with a server error response failure. (type: Deferred which successfully fires with bytes or fails with ServerErrorResponse ) |