imaplib --- IMAP4 協定用戶端¶
原始碼:Lib/imaplib.py
This module defines three classes, IMAP4, IMAP4_SSL and
IMAP4_stream, which encapsulate a connection to an IMAP4 server and
implement a large subset of the IMAP4rev1 client protocol as defined in
RFC 2060. It is backward compatible with IMAP4 (RFC 1730) servers, but
note that the STATUS command is not supported in IMAP4.
可用性: not WASI.
此模組在 WebAssembly 平台上不起作用或無法使用。更多資訊請參閱 WebAssembly 平台。
Three classes are provided by the imaplib module, IMAP4 is the
base class:
- class imaplib.IMAP4(host='', port=IMAP4_PORT, timeout=None)¶
This class implements the actual IMAP4 protocol. The connection is created and protocol version (IMAP4 or IMAP4rev1) is determined when the instance is initialized. If host is not specified,
''(the local host) is used. If port is omitted, the standard IMAP4 port (143) is used. The optional timeout parameter specifies a timeout in seconds for the connection attempt. If timeout is not given or isNone, the global default socket timeout is used.The
IMAP4class supports thewithstatement. When used like this, the IMAP4LOGOUTcommand is issued automatically when thewithstatement exits. E.g.:>>> from imaplib import IMAP4 >>> with IMAP4("domain.org") as M: ... M.noop() ... ('OK', [b'Nothing Accomplished. d25if65hy903weo.87'])
在 3.5 版的變更: 新增對
with陳述式的支援。在 3.9 版的變更: 新增 timeout 選用參數。
Three exceptions are defined as attributes of the IMAP4 class:
- exception IMAP4.error¶
Exception raised on any errors. The reason for the exception is passed to the constructor as a string.
- exception IMAP4.abort¶
IMAP4 server errors cause this exception to be raised. This is a sub-class of
IMAP4.error. Note that closing the instance and instantiating a new one will usually allow recovery from this exception.
- exception IMAP4.readonly¶
This exception is raised when a writable mailbox has its status changed by the server. This is a sub-class of
IMAP4.error. Some other client now has write permission, and the mailbox will need to be re-opened to re-obtain write permission.
There's also a subclass for secure connections:
- class imaplib.IMAP4_SSL(host='', port=IMAP4_SSL_PORT, *, ssl_context=None, timeout=None)¶
This is a subclass derived from
IMAP4that connects over an SSL encrypted socket (to use this class you need a socket module that was compiled with SSL support). If host is not specified,''(the local host) is used. If port is omitted, the standard IMAP4-over-SSL port (993) is used. ssl_context is assl.SSLContextobject which allows bundling SSL configuration options, certificates and private keys into a single (potentially long-lived) structure. Please read Security considerations for best practices.The optional timeout parameter specifies a timeout in seconds for the connection attempt. If timeout is not given or is
None, the global default socket timeout is used.在 3.3 版的變更: 新增 ssl_context 參數。
在 3.4 版的變更: The class now supports hostname check with
ssl.SSLContext.check_hostnameand Server Name Indication (seessl.HAS_SNI).在 3.9 版的變更: 新增 timeout 選用參數。
在 3.12 版的變更: The deprecated keyfile and certfile parameters have been removed.
The second subclass allows for connections created by a child process:
- class imaplib.IMAP4_stream(command)¶
This is a subclass derived from
IMAP4that connects to thestdin/stdoutfile descriptors created by passing command tosubprocess.Popen().
The following utility functions are defined:
- imaplib.Internaldate2tuple(datestr)¶
Parse an IMAP4
INTERNALDATEstring and return corresponding local time. The return value is atime.struct_timetuple orNoneif the string has wrong format.
- imaplib.Int2AP(num)¶
Converts an integer into a bytes representation using characters from the set [
A..P].
- imaplib.ParseFlags(flagstr)¶
Converts an IMAP4
FLAGSresponse to a tuple of individual flags.
- imaplib.Time2Internaldate(date_time)¶
Convert date_time to an IMAP4
INTERNALDATErepresentation. The return value is a string in the form:"DD-Mmm-YYYY HH:MM:SS +HHMM"(including double-quotes). The date_time argument can be a number (int or float) representing seconds since epoch (as returned bytime.time()), a 9-tuple representing local time an instance oftime.struct_time(as returned bytime.localtime()), an aware instance ofdatetime.datetime, or a double-quoted string. In the last case, it is assumed to already be in the correct format.
Note that IMAP4 message numbers change as the mailbox changes; in particular,
after an EXPUNGE command performs deletions the remaining messages are
renumbered. So it is highly advisable to use UIDs instead, with the UID command.
At the end of the module, there is a test section that contains a more extensive example of usage.
也參考
Documents describing the protocol, sources for servers implementing it, by the University of Washington's IMAP Information Center can all be found at (Source Code) https://github.com/uw-imap/imap (Not Maintained).
IMAP4 物件¶
All IMAP4rev1 commands are represented by methods of the same name, either uppercase or lowercase.
All arguments to commands are converted to strings, except for AUTHENTICATE,
and the last argument to APPEND which is passed as an IMAP4 literal. If
necessary (the string contains IMAP4 protocol-sensitive characters and isn't
enclosed with either parentheses or double quotes) each string is quoted.
However, the password argument to the LOGIN command is always quoted. If
you want to avoid having an argument string quoted (eg: the flags argument to
STORE) then enclose the string in parentheses (eg: r'(\Deleted)').
Most commands return a tuple: (type, [data, ...]) where type is usually
'OK' or 'NO', and data is either the text from the command response,
or mandated results from the command. Each data is either a bytes, or a
tuple. If a tuple, then the first part is the header of the response, and the
second part contains the data (ie: 'literal' value).
The message_set options to commands below is a string specifying one or more
messages to be acted upon. It may be a simple message number ('1'), a range
of message numbers ('2:4'), or a group of non-contiguous ranges separated by
commas ('1:3,6:9'). A range can contain an asterisk to indicate an infinite
upper bound ('3:*').
IMAP4 實例擁有以下方法:
- IMAP4.append(mailbox, flags, date_time, message)¶
Append message to named mailbox.
- IMAP4.authenticate(mechanism, authobject)¶
Authenticate command --- requires response processing.
mechanism specifies which authentication mechanism is to be used - it should appear in the instance variable
capabilitiesin the formAUTH=mechanism.authobject must be a callable object:
data = authobject(response)
It will be called to process server continuation responses; the response argument it is passed will be
bytes. It should returnbytesdata that will be base64 encoded and sent to the server. It should returnNoneif the client abort response*should be sent instead.在 3.5 版的變更: string usernames and passwords are now encoded to
utf-8instead of being limited to ASCII.
- IMAP4.check()¶
Checkpoint mailbox on server.
- IMAP4.close()¶
Close currently selected mailbox. Deleted messages are removed from writable mailbox. This is the recommended command before
LOGOUT.
- IMAP4.copy(message_set, new_mailbox)¶
Copy message_set messages onto end of new_mailbox.
- IMAP4.create(mailbox)¶
Create new mailbox named mailbox.