Python asyncio IMAP4rev1 client library
The aim is to port the imaplib with asyncio, to benefit from the sleep or treat model.
It runs with python 3.4 and 3.5.
import asyncio from aioimaplib import aioimaplib @asyncio.coroutine def check_mailbox(host, user, password): imap_client = aioimaplib.IMAP4_SSL(host=host) yield from imap_client.wait_hello_from_server() yield from imap_client.login(user, password) res, data = yield from imap_client.select() print('there is %s messages INBOX' % data) yield from imap_client.logout() if __name__ == '__main__': loop = asyncio.get_event_loop() loop.run_until_complete(check_mailbox('my.imap.server', 'user', 'pass'))
The RFC2177 is implemented, to be able to wait for new mail messages without using CPU. The responses are pushed in an async queue, and it is possible to read them in real time. To leave the IDLE mode, it is necessary to send a “DONE” command to the server.
@asyncio.coroutine def wait_for_new_message(host, user, password): imap_client = aioimaplib.IMAP4_SSL(host=host) yield from imap_client.wait_hello_from_server() yield from imap_client.login(user, password) yield from imap_client.select() asyncio.async(imap_client.idle()) while True: msg = yield from imap_client.wait_server_push() print('--> received from server: %s' % msg) if 'EXISTS' in msg: imap_client.idle_done() break yield from imap_client.logout() if __name__ == '__main__': loop = asyncio.get_event_loop() loop.run_until_complete(wait_for_new_message('my.imap.server', 'user', 'pass'))
It is possible to use threads but each IMAP4ClientProtocol instance should run in the same thread :
Each color rectangle is an IMAP4ClientProtocol instance piece of code executed by the thread asyncio loop until it reaches a yield, waiting on I/O.
For example, it is possible to launch 4 mono-threaded mail-fetcher processes on a 4 cores server with supervisor, and use a distribution function like len(email) % (process_num) or whatever to share equally a mail account list between the 4 processes.
IMAP command concurrency
IMAP protocol allows to run some commands in parallel. Four rules are implemented to ensure responses consistency:
- if a sync command is running, the following requests (sync or async) must wait
- if an async command is running, same async commands (or with the same untagged response type) must wait
- async commands can be executed in parallel
- sync command must wait pending async commands to finish
- dovecot 2.2.13 on debian Jessie
Developers are welcome ! If you want to improve it, fix bugs, test it with other IMAP servers, give feedback, thank you for it.
To develop, just run:
virtualenv --python=python3.4 venv source venv/bin/activate python setup.py develop pip install -r dev-requirements.txt nosetests
To add a imaplib or imaplib2 command you can :
- add the function to the testing imapserver with a new imaplib or imaplib2 server test, i.e. test_imapserver_imaplib.py or test_imapserver_imaplib2.py respectively;
- then add the function to the aioimaplib doing almost the same test than above but the async way in test_aioimaplib.py.
Not unit tested
- 23/25 IMAP4rev1 commands are implemented from the main rfc3501. ‘STARTTLS’ and ‘AUTHENTICATE’ are still missing.
- ‘COMPRESS’ from rfc4978
- ‘SETACL’ ‘DELETEACL’ ‘GETACL’ ‘MYRIGHTS’ ‘LISTRIGHTS’ from ACL rfc4314
- ‘GETQUOTA’: ‘GETQUOTAROOT’: ‘SETQUOTA’ from quota rfc2087
- ‘SORT’ and ‘THREAD’ from the rfc5256
- ‘ID’ from the rfc2971
- ‘NAMESPACE’ from rfc2342
- ‘CATENATE’ from rfc4469
- ‘WITHIN’ from rfc5032
- tests with other servers
If it goes wrong
Sometimes you break things and you don’t understand what’s going on (I always do). For this library I have two related tools:
- ngrep on the imap test port : sudo ngrep -d lo port 12345
- activate debug logs changing INFO to DEBUG at the top of the mock server and the aioimaplib
- refactor: treating response as we read the imap server responses for a better reading
- removing tests from package
- publish on pypi
- added coverall
- fix aioimaplib bug when receiving chunked fetch data
- do not abort when receiving unsollicited data from server
- build CI environment
- license GPL v3.0
- added APPEND command
- fix usernames can have ‘@’ for mockimapserver
- server can handle SEARCH with CHARSET opt parameter (but ignores it)
- added 11 new imap commands
- added imap command synchronizing
- init project with mockimapserver
- project files
- 11 imap commands
Release history Release notifications
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size & hash SHA256 hash help||File type||Python version||Upload date|
|aioimaplib-0.5.4-py3.4.egg (63.0 kB) Copy SHA256 hash SHA256||Egg||3.4|
|aioimaplib-0.5.4-py3-none-any.whl (30.3 kB) Copy SHA256 hash SHA256||Wheel||3.4|
|aioimaplib-0.5.4.tar.gz (47.7 kB) Copy SHA256 hash SHA256||Source||None|