iPhone: getting IMAP push notifications with Python and Prowl

I’m a happy iPhone user. I was lucky to get the first iPhone soon after it was available and I just bought a new iPhone4. This is my first time having unlimited data plan on a mobile phone 🙂 so first thing I did was to configure my email accounts, both personal and work.
It felt weird to configure a GMail account as Microsoft Exchange in order to sync contacts and get push notifications, but that did the trick. For my work email account, I didn’t have push notifications, and polling is not any cool. I know very little about email itself, but I found that in order to have push email you need an IMAP IDLE compatible server and client.
As you may see below, the server does support IMAP IDLE:
[gist]https://gist.github.com/579679[/gist]
but it looks like iPhone Mail app doesn’t 🙁
I started looking around and finally found the solution: use an application called Prowl (2.39€) which will push whatever notification it gets through a REST API. The idea is basically to create a console IMAP IDLE client and use the Prowl API to send a new notification. Sounds like fun!
There is a Perl implementation here (http://github.com/mschmitt/GhettoPush/) but I don’t like Perl very much, so I had to do it in Python, of course 😉
I also found some other Python implementations which I didn’t like for one reason or another so I coded a new one using imaplib2 by Piers Lauder (http://www.cs.usyd.edu.au/~piers/python/imaplib.html) and prowlpy, which I forked on GitHub and did a couple of really silly modifications (http://github.com/saghul/prowlpy).
It works on the background and it can connect to an arbitrary name of accounts (configured with an ini-style file) and send push notifications on behalf of them. It also logs to syslog, if running in the background.
This Prowl thing is just awesome, this is an example of what you can do with it, but anyone can write a really simple script to get instant notifications on his/her iPhone. Just give it a try!
The libraries:
References:

 

Happy pushing!