class 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.
Method | apop |
Send an APOP command to perform authenticated login. |
Method | capabilities |
Send a CAPA command to retrieve the capabilities supported by the server. |
Method | connection |
Clean up when the connection has been lost. |
Method | connection |
Wait for a greeting from the server after the connection has been made. |
Method | delete |
Send a DELE command to delete a message from the server. |
Method | line |
Drop the connection when a server response exceeds the maximum line length (LineOnlyReceiver.MAX_LENGTH ). |
Method | line |
Pass a received line to a state machine function and transition to the next state. |
Method | list |
Send a LIST command to retrieve the sizes of all messages on the server. |
Method | list |
Send a UIDL command to retrieve the UIDs of all messages on the server. |
Method | login |
Log in to the server. |
Method | noop |
Send a NOOP command asking the server to do nothing but respond. |
Method | password |
Send a PASS command to perform the second half of plaintext login. |
Method | quit |
Send a QUIT command to disconnect from the server. |
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 | send |
Send a POP3 command to which a multi-line response is expected. |
Method | send |
Send a POP3 command to which a short response is expected. |
Method | server |
Handle the server greeting. |
Method | start |
Switch to encrypted communication using TLS. |
Method | stat |
Send a STAT command to get information about the size of the mailbox. |
Method | state_ |
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 | state_ |
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_ |
Handle server responses for the SHORT state in which the server is expected to send a single line response. |
Method | state_ |
Log an error for server responses received in the WAITING state during which the server is not expected to send anything. |
Method | state_ |
Handle server responses for the WELCOME state in which the server greeting is expected. |
Method | timeout |
Drop the connection when the server does not respond in time. |
Method | user |
Send a USER command to perform the first half of plaintext login. |
Instance Variable | allow |
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 | server |
The challenge received in the server greeting. |
Instance Variable | started |
An indication of whether TLS has been negotiated successfully. |
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'. |
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 | transport |
Undocumented |
Method | _apop |
Perform an APOP login. |
Method | _blocked |
Block a command, if necessary. |
Method | _consume |
Send a command to which a long response is expected and process the multi-line response into a list. |
Method | _consume |
Send a command to which a long response is expected and process the multi-line response into a list accounting for deleted messages. |
Method | _get |
Get a context factory with which to negotiate TLS. |
Method | _login |
Continue the process of logging in to the server. |
Method | _login |
Do a plaintext login over an encrypted transport. |
Method | _plaintext |
Perform a plaintext login. |
Method | _started |
Complete the process of switching to encrypted communication. |
Method | _start |
Continue the process of switching to encrypted communication. |
Method | _unblock |
Send the next blocked command. |
Instance Variable | _blocked |
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... |
Instance Variable | _cap |
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 | _challenge |
A regular expression which matches the challenge in the server greeting. |
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. |
Instance Variable | _greeting |
The server greeting minus the status indicator, when the connection was dropped because of an error in the server greeting. Otherwise, None . |
Instance Variable | _timed |
An indication of whether the connection was dropped because of a timeout. |
Instance Variable | _waiting |
A deferred which fires when the response to the outstanding command is received from the server. |
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. |
Inherited from LineOnlyReceiver
:
Method | data |
Translates bytes into lines, and calls lineReceived. |
Method | send |
Sends a line to the other end of the connection. |
Constant | MAX |
The maximum length of a line to allow (If a sent line is longer than this, the connection is dropped). Default is 16384. |
Class Variable | delimiter |
The line-ending delimiter to use. By default this is b'\r\n'. |
Instance Variable | _buffer |
Undocumented |
Inherited from Protocol
(via LineOnlyReceiver
):
Method | log |
Return a prefix matching the class name, to identify log messages related to this protocol instance. |
Class Variable | factory |
Undocumented |
Inherited from BaseProtocol
(via LineOnlyReceiver
, Protocol
):
Method | make |
Make a connection to a transport and a server. |
Instance Variable | connected |
Undocumented |
Inherited from TimeoutMixin
(via LineOnlyReceiver
, Protocol
, BaseProtocol
):
Method | call |
Wrapper around reactor.callLater for test purpose. |
Method | reset |
Reset the timeout count down. |
Method | set |
Change the timeout period |
Instance Variable | time |
The number of seconds after which to timeout the connection. |
Method | __timed |
Undocumented |
Instance Variable | __timeout |
Undocumented |
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:bytes | The username with which to log in. |
digest:bytes | The challenge response to authenticate with. |
Returns | |
Deferred which successfully fires with bytes or fails with ServerErrorResponse | 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. |
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 | |
usebool | A flag that determines whether previously retrieved results should be used if available. |
Returns | |
Deferred which successfully results in dict mapping bytes to list of bytes and/or bytes to None | 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']} |
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:Failure | The reason the connection was terminated. |
Wait for a greeting from the server after the connection has been made.
Start the connection in the WELCOME state.
Send a DELE command to delete a message from the server.
Parameters | |
index:int | The 0-based index of the message to delete. |
Returns | |
Deferred which successfully fires with bytes or fails with ServerErrorResponse | 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. |
Drop the connection when a server response exceeds the maximum line length (LineOnlyReceiver.MAX_LENGTH
).
Parameters | |
buffer:bytes | A received line which exceeds the maximum line length. |
Pass a received line to a state machine function and transition to the next state.
Parameters | |
line:bytes | A received line. |
Send a LIST command to retrieve the sizes of all messages on the server.
Parameters | |
consumer:None or callable that takes 2-tuple of (0) int , (1) int | A function which consumes the 0-based message index and message size derived from the server response. |
Returns | |
Deferred which fires list of int or callable that takes 2-tuple of (0) int , (1) int | 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. |
Send a UIDL command to retrieve the UIDs of all messages on the server.
Parameters | |
consumer:None or callable that takes 2-tuple of (0) int , (1) bytes | A function which consumes the 0-based message index and UID derived from the server response. |
Returns | |
Deferred which fires with list of object or callable that takes 2-tuple of (0) int , (1) bytes | 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. |
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:bytes | The username with which to log in. |
password:bytes | The password with which to log in. |
Returns | |
Deferred which successfully fires with bytes | A deferred which fires when the login process is complete. On a successful login, it returns the server's response minus the status indicator. |
Send a NOOP command asking the server to do nothing but respond.
Returns | |
Deferred which successfully fires with bytes or fails with ServerErrorResponse | 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. |
Send a PASS command to perform the second half of plaintext login.
Unless this is absolutely required, use the login
method instead.
Parameters | |
password:bytes | The plaintext password with which to authenticate. |
Returns | |
Deferred which successfully fires with bytes or fails with ServerErrorResponse | 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. |
Send a QUIT command to disconnect from the server.
Returns | |
Deferred which successfully fires with bytes or fails with ServerErrorResponse | 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. |
Send a RSET command to unmark any messages that have been flagged for deletion on the server.
Returns | |
Deferred which successfully fires with bytes or fails with ServerErrorResponse | 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. |
Send a RETR or TOP command to retrieve all or part of a message from the server.
Parameters | |
index:int | A 0-based message index. |
consumer:None or callable that takes bytes | A function which consumes each transformed line from a multi-line response as it is received. |
lines:None or int | If specified, the number of lines of the message to be retrieved. Otherwise, the entire message is retrieved. |
Returns | |
Deferred which fires with list of bytes , or callable that takes 2-tuple of (0) int , (1) object | 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. |
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:bytes | A POP3 command. |
args:bytes | The command arguments. |
consumer:callable that takes object | 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. |
xform:None or callable that takes bytes and returns object | 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. |
Returns | |
Deferred which successfully fires with callable that takes object and fails with ServerErrorResponse | 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. |
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:bytes | A POP3 command. |
args:bytes | The command arguments. |
Returns | |
Deferred which successfully fires with bytes or fails with ServerErrorResponse | 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. |
Handle the server greeting.
Parameters | |
greeting:bytes | The server greeting minus the status indicator. For servers implementing APOP authentication, this will contain a challenge string. |
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 | |
contextNone or ClientContextFactory | The context factory with which to negotiate TLS. If not provided, try to create a new one. |
Returns | |
Deferred which successfully results in dict mapping bytes to list of bytes and/or bytes to None or fails with TLSError | 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. |
Send a STAT command to get information about the size of the mailbox.
Returns | |
Deferred which successfully fires with a 2-tuple of (0) int , (1) int or fails with ServerErrorResponse | 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. |
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:bytes | A line received from the server. |
Returns | |
bytes | The next state. |
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:bytes | A line received from the server. |
Returns | |
bytes | The next state. |
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:bytes | A line received from the server. |
Returns | |
bytes | The next state. |
Log an error for server responses received in the WAITING state during which the server is not expected to send anything.
Parameters | |
line:bytes | A line received from the server. |
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:bytes | A line received from the server. |
Returns | |
bytes | The next state. |
Send a USER command to perform the first half of plaintext login.
Unless this is absolutely required, use the login
method instead.
Parameters | |
username:bytes | The username with which to log in. |
Returns | |
Deferred which successfully fires with bytes or fails with ServerErrorResponse | 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. |
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.
The state which indicates what type of response is expected from the server. Valid states are: 'WELCOME', 'WAITING', 'SHORT', 'LONG_INITIAL', 'LONG'.
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.
Perform an APOP login.
Parameters | |
username:bytes | The username with which to log in. |
password:bytes | The password with which to log in. |
challenge:bytes | A challenge string. |
Returns | |
Deferred which successfully fires with bytes or fails with ServerErrorResponse | A deferred which fires when the server response is received. On a successful login, it returns the server response minus the status indicator. |
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:callable | A function which sends a command. |
*a:tuple | Arguments to the function. |
Returns | |
None or Deferred | None if the command can run immediately. Otherwise, a deferred that will eventually trigger with the return value of the function. |
Send a command to which a long response is expected and process the multi-line response into a list.
Parameters | |
cmd:bytes | A POP3 command which expects a long response. |
args:bytes | The command arguments. |
consumer:None or callable that takes object | None or a function that consumes the output from the transform function. |
xform:None or callable that takes bytes and returns object | 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. |
Returns | |
Deferred which fires with list of 2-tuple of (0) int , (1) object or callable that takes 2-tuple of (0) int , (1) object | 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. |
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:bytes | A POP3 command to which a long response is expected. |
args:bytes | The command arguments. |
consumer:None or callable that takes object | None or a function that consumes the output from the transform function. |
xform:None , callable that takes bytes and returns 2-tuple of (0) int , (1) object , or callable that takes bytes and returns object | 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. |
Returns | |
Deferred which fires with list of object or callable that takes list of object | 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. |
Get a context factory with which to negotiate TLS.
Returns | |
None or ClientContextFactory | A context factory or None if TLS is not supported on the client. |
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:dict mapping bytes to list of bytes and/or bytes to None | The server capabilities. |
username:bytes | The username with which to log in. |
password:bytes | The password with which to log in. |
Returns | |
Deferred which successfully fires with bytes | A deferred which fires when the login process is complete. On a successful login, it returns the server's response minus the status indicator. |
Do a plaintext login over an encrypted transport.
This callback function runs after the transport switches to encrypted communication.
Parameters | |
res:dict mapping bytes to list of bytes and/or bytes to None | The server capabilities. |
username:bytes | The username with which to log in. |
password:bytes | The password with which to log in. |
Returns | |
Deferred which successfully fires with bytes or fails with ServerErrorResponse | 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. |
Perform a plaintext login.
Parameters | |
username:bytes | The username with which to log in. |
password:bytes | The password with which to log in. |
Returns | |
Deferred which successfully fires with bytes or fails with ServerErrorResponse | 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. |
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:dict mapping bytes to list of bytes and/or bytes to None | The server capabilities. |
context:ClientContextFactory | A context factory with which to negotiate TLS. |
tls:ITLSTransport | A TCP transport that supports switching to TLS midstream. |
Returns | |
dict mapping bytes to list of bytes and/or bytes to None | The server capabilities. |
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:dict mapping bytes to list of bytes and/or bytes to None | The server capabilities. |
contextClientContextFactory | A context factory with which to negotiate TLS. |
tls:ITLSTransport | A TCP transport that supports switching to TLS midstream. |
Returns | |
Deferred which successfully triggers with dict mapping bytes to list of bytes and/or bytes to None or fails with TLSNotSupportedError | 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. |
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.
None
or list
of 3-tuple
of (0) Deferred
, (1) callable which results in a Deferred
, (2) tuple
=
(source)
¶
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.
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.
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.
The server greeting minus the status indicator, when the connection was dropped because of an error in the server greeting. Otherwise, None
.