-
Notifications
You must be signed in to change notification settings - Fork 81
/
idle_selector_example.py
33 lines (30 loc) · 1.2 KB
/
idle_selector_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from datetime import datetime, timedelta
from selectors import DefaultSelector, EVENT_READ
from imapclient import IMAPClient
HOST = "localhost"
USERNAME = "user"
PASSWORD = "Tr0ub4dor&3"
RESPONSE_TIMEOUT_SECONDS = 15
IDLE_SECONDS = 60 * 24
with IMAPClient(HOST, timeout=RESPONSE_TIMEOUT_SECONDS) as server:
server.login(USERNAME, PASSWORD)
server.select_folder("INBOX", readonly=True)
server.idle()
print("Connection is now in IDLE mode," " send yourself an email or quit with ^c")
try:
with DefaultSelector() as selector:
selector.register(server.socket(), EVENT_READ, None)
now = datetime.now
end_at = now() + timedelta(seconds=IDLE_SECONDS)
while selector.select((end_at - now()).total_seconds()):
responses = server.idle_check(timeout=0)
if not responses:
raise ConnectionError(
"Socket readable without data. Likely closed."
)
print("Server sent:", responses)
print("IDLE time out.")
except KeyboardInterrupt:
print("") # Newline after the typically echoed ^C.
server.idle_done()
print("IDLE mode done")