File: idle_selector_example.py

package info (click to toggle)
python-imapclient 3.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 668 kB
  • sloc: python: 5,355; sh: 14; makefile: 11
file content (33 lines) | stat: -rw-r--r-- 1,229 bytes parent folder | download
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")