Debian Codegraph @

doc/src/advanced.rst

raw
1 Advanced Usage
2 --------------
3 This document covers some more advanced features and tips for handling
4 specific usages.
5
6 Cleaning Up Connections
7 ~~~~~~~~~~~~~~~~~~~~~~~
8
9 To communicate with the server, IMAPClient establishes a TCP connection. It is
10 important for long-lived processes to always close connections at some
11 point to avoid leaking memory and file descriptors. This is usually done with
12 the ``logout`` method::
13
14 import imapclient
15
16 c = imapclient.IMAPClient(host="imap.foo.org")
17 c.login("[email protected]", "passwd")
18 c.select_folder("INBOX")
19 c.logout()
20
21 However if an error is raised when selecting the folder, the connection may be
22 left open.
23
24 IMAPClient may be used as a context manager that automatically closes
25 connections when they are not needed any more::
26
27 import imapclient
28
29 with imapclient.IMAPClient(host="imap.foo.org") as c:
30 c.login("[email protected]", "passwd")
31 c.select_folder("INBOX")
32
33 Watching a Mailbox Using IDLE
34 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35 The IDLE extension allows an IMAP server to notify a client when something
36 changes in a mailbox. It can be used as an alternative to polling to receive
37 new messages.
38
39 The concept is simple: the client connects to the server, selects a mailbox and
40 enters the IDLE mode. At this point the server sends notifications whenever
41 something happens in the selected mailbox until the client ends the IDLE mode
42 by issuing a ``DONE`` command. This is explained in :rfc:`2177`.
43
44 .. literalinclude:: ../../examples/idle_example.py
45
46 Note that IMAPClient does not handle low-level socket errors that can happen
47 when maintaining long-lived TCP connections. Users are advised to renew the
48 IDLE command every 10 minutes to avoid the connection from being abruptly
49 closed.
50
51 Interactive Sessions
52 ~~~~~~~~~~~~~~~~~~~~
53 When developing program using IMAPClient is it sometimes useful to
54 have an interactive shell to play with. IMAPClient ships with a module
55 that lets you fire up an interactive shell with an IMAPClient instance
56 connected to an IMAP server.
57
58 Start a session like this::
59
60 python -m imapclient.interact -H <host> -u <user> ...
61
62 Various options are available to specify the IMAP server details. See
63 the help (--help) for more details. You'll be prompted for a username
64 and password if one isn't provided on the command line.
65
66 It is also possible to pass connection details as a configuration file
67 like this::
68
69 python -m imapclient.interact -f <config file>
70
71 See below for details of the :ref:`configuration file format<conf-files>`.
72
73 If installed, IPython will be used as the embedded shell. Otherwise
74 the basic built-in Python shell will be used.
75
76 The connected IMAPClient instance is available as the variable
77 "c". Here's an example session::
78
79 $ python -m imapclient.interact -H <host> -u <user> ...
80 Connecting...
81 Connected.
82
83 IMAPClient instance is "c"
84 In [1]: c.select_folder('inbox')
85 Out[1]:
86 {b'EXISTS': 2,
87 b'FLAGS': (b'\\Answered',
88 b'\\Flagged',
89 b'\\Deleted',
90 b'\\Seen',
91 b'\\Draft'),
92 b'PERMANENTFLAGS': (b'\\Answered',
93 b'\\Flagged',
94 b'\\Deleted',
95 b'\\Seen',
96 b'\\Draft'),
97 b'READ-WRITE': True,
98 b'RECENT': 0,
99 b'UIDNEXT': 1339,
100 b'UIDVALIDITY': 1239278212}
101
102 In [2]: c.search()
103 Out[2]: [1123, 1233]
104
105 In [3]: c.logout()
106 Out[3]: b'Logging out'
107
108
109 .. _conf-files:
110
111 Configuration File Format
112 +++++++++++++++++++++++++
113 Both the IMAPClient interactive shell and the live tests take
114 configuration files which specify how to to connect to an IMAP
115 server. The configuration file format is the same for both.
116
117 Configuration files use the INI format and must always have a section
118 called ``DEFAULT``. Here's a simple example::
119
120 [DEFAULT]
121 host = imap.mailserver.com
122 username = bob
123 password = sekret
124 ssl = True
125
126 The supported options are:
127
128 ==================== ======= =========================================================================================
129 Name Type Description
130 ==================== ======= =========================================================================================
131 host string IMAP hostname to connect to.
132 username string The username to authenticate as.
133 password string The password to use with ``username``.
134 port int Server port to connect to. Defaults to 143 unless ``ssl`` is True.
135 ssl bool Use SSL/TLS to connect.
136 starttls bool Use STARTTLS to connect.
137 ssl_check_hostname bool If true and SSL is in use, check that certificate matches the hostname (defaults to true)
138 ssl_verify_cert bool If true and SSL is in use, check that the certificate is valid (defaults to true).
139 ssl_ca_file string If SSL is true, use this to specify certificate authority certs to validate with.
140 timeout int Time out I/O operations after this many seconds.
141 oauth2 bool If true, use OAUTH2 to authenticate (``username`` and ``password`` are ignored).
142 oauth2_client_id string OAUTH2 client id.
143 oauth2_client_secret string OAUTH2 client secret.
144 oauth2_refresh_token string OAUTH2 token for refreshing the secret.
145 ==================== ======= =========================================================================================
146
147 Acceptable boolean values are "1", "yes", "true", and "on", for true;
148 and "0", "no", "false", and "off", for false.