Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bitdivine/3108a62b0c28a501ba81 to your computer and use it in GitHub Desktop.
Save bitdivine/3108a62b0c28a501ba81 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
These are the core lines extracted from a live, working torrenter that
uses DHT to avoid a single point of faliure, the torrent server.
Adapt to suit your needs and enjoy. Please read the docs.
Prerequisites:
PORT_RANGE must be open for bidirectional TCP and UDP traffic.
libtorrent needs to be installed. Something like:
apt-get install python-libtorrent
If hosts are on the same local network they should discover each other.
Otherwise you will have to tell each of the hosts about at least one
other torrenter. The distributed hash table will complete the picture.
References:
http://www.rasterbar.com/products/libtorrent/manual.html#start-dht-stop-dht-set-dht-settings-dht-state-is-dht-running
http://www.bittorrent.org/beps/bep_0005.html
https://github.com/arvidn/libtorrent-daemon/blob/84e486bc737b876ce30cb1491d3f966fe921ec91/main.cpp
http://stackoverflow.com/questions/1332107/how-does-dht-in-torrents-work
"""
import libtorrent as lt
PORT_RANGE = (6881,6891) # This is standard but who said that you have to be? :-)
ses = lt.session()
ses.listen_on(PORT_RANGE[0],PORT_RANGE[1])
# Distributed hash:
dht_hosts=[ ('myotherserver',6881), ('myotherotherserver',6881)]
for (host,port) in dht_hosts:
ses.add_dht_router(host,port)
ses.start_dht()
# Add a torrent:
e = lt.bdecode(open('somefile.torrent', 'rb').read())
info = lt.torrent_info(e)
params = { "save_path":'/home/db/torrents',"storage_mode": lt.storage_mode_t.storage_mode_sparse,"ti":info}
ses.add_torrent(params)
# Chug away:
while True:
PROCESS FILES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment