Skip to content

Instantly share code, notes, and snippets.

@codeboy
Created February 1, 2015 15:50
Show Gist options
  • Save codeboy/923f74b72abda9502f42 to your computer and use it in GitHub Desktop.
Save codeboy/923f74b72abda9502f42 to your computer and use it in GitHub Desktop.
A very simple example of python code to us with python-libtorrent
'''
You should try libtorrent (rasterbar). http://libtorrent.org
If you want to write your client in python, on linux, install it with:
sudo apt-get install python-libtorrent
A very simple example of python code to use it to download a torrent:
'''
import libtorrent as lt
import time
import sys
ses = lt.session()
ses.listen_on(6881, 6891)
info = lt.torrent_info(sys.argv[1])
h = ses.add_torrent({'ti': info, 'save_path': './'})
print 'starting', h.name()
while (not h.is_seed()):
s = h.status()
state_str = ['queued', 'checking', 'downloading metadata', \
'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
print '\r%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
(s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
s.num_peers, state_str[s.state]),
sys.stdout.flush()
time.sleep(1)
print h.name(), 'complete'
@fastesol
Copy link

fastesol commented Aug 6, 2017

I'm trying with the following code but it stuck in the while loop while (not h.has_metadata())

import libtorrent as lt
import time
import sys

ses = lt.session()
ses.listen_on(6881, 6891)
params = {'save_path': '.'}


ses.add_dht_router("router.utorrent.com", 6881)
ses.add_dht_router("router.bittorrent.com", 6881)
ses.add_dht_router("dht.transmissionbt.com", 6881)
ses.start_dht()

link = "magnet:?xt=urn:btih:648bee814b508e05c46fe13869c4d71dfe5c27b4&dn=The+Equalizer+%282014%29+HDCAM+READNFO+x264+AC3-CPG&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.istole.it%3A6969&tr=udp%3A%2F%2Fopen.demonii.com%3A1337"
h = lt.add_magnet_uri(ses, link, params)

while (not h.has_metadata()):
	time.sleep(1)

torinfo = h.get_torrent_info()

any thought please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment