imaplib - How to check whether IMAP-IDLE works? -
i noticed imap server seems support idle notifications arrive late. asking myself: how can check whether idle works (or mail client)?
inspired http://pymotw.com/2/imaplib/, can use following python scripts check if , how fast push notification via idle work:
imaplib_connect.py
import imaplib import configparser import os def open_connection(verbose=false): # read config file config = configparser.configparser() config.read([os.path.abspath('settings.ini')]) # connect server hostname = config.get('server', 'hostname') if verbose: print 'connecting to', hostname connection = imaplib.imap4_ssl(hostname) # login our account username = config.get('account', 'username') password = config.get('account', 'password') if verbose: print 'logging in as', username connection.login(username, password) return connection if __name__ == '__main__': c = open_connection(verbose=true) try: print c finally: c.logout() print "logged out"
imaplib_idlewait.py
import imaplib import pprint import imaplib_connect imaplib.debug = 4 c = imaplib_connect.open_connection() try: c.select('inbox', readonly=true) c.send("%s idle\r\n"%(c._new_tag())) print ">>> waiting new mail..." while true: line = c.readline().strip(); if line.startswith('* bye ') or (len(line) == 0): print ">>> leaving..." break if line.endswith('exists'): print ">>> new mail arrived!" finally: try: print ">>> closing..." c.close() except: pass c.logout()
settings.ini
[server] hostname: yourserver.com [account] username: yourmail@yourserver.com password: yoursecretpassword
after creating files, call
python imaplib_idlewait.py
please note, scripts not close gracefully if press ctrl+c (readline() blocking , not terminated close()), however, testing should enough.
also note, mail server terminate connection after 30 minutes. after have re-open connection, e.g. demonstrated here: http://blog.mister-muffin.de/2013/06/05/reliable-imap-synchronization-with-idle-support
Comments
Post a Comment