Making a Minimalist Bittorrent Client

I spent the weekend sorting out bittorrent support in Peapod so that it
wasn't quite such a mess. Previously I had hacked a version of
btdownloadheadless.py so that it would exit when the download
completed. I was then shelling out to this from within Peapod.
Obviously this is a less than ideal situation partly because it meant I had to escape URLs from the shell. So I hacked away at btdownloadheadless.py until I had a simple class I could call to do the bittorrent download. It gives almost no feedback
except a return code upon completion of the download.

from btclient import mytorrent
url = "http://www.evilgeniuschronicles.org/audio/egc-2005-10-10.mp3.torrent"
save_dir  = /tmp
torrent = mytorrent(url,save_dir)
ret = torrent.run()
sys.exit(ret)

If you wanted to carry on seeding rather than exiting on completion you could do:

torrent = mytorrent(url,save_dir,selfish=0)
torrent.run()

I then added a name == "__main__" section into btclient so that it can be used a simple bittorrent client.
btclient.py http://www.evilgeniuschronicles.org/audio/egc-2005-10-10.mp3.torrent /tmp
or to seed:
btclient.py http://www.evilgeniuschronicles.org/audio/egc-2005-10-10.mp3.torrent /tmp seed

btclient.py  can be downloaded from here or alternatively see our subversion repository where the latest version can always be found.

Hopefully someone will find this useful.