Downloading a Torrent with libtorrent-python
Asked Answered
E

2

18

I have the following python code:

import libtorrent as lt
import time

ses = lt.session()
ses.listen_on(6881, 6891)
params = {
    'save_path': '/home/downloads/',
    'storage_mode': lt.storage_mode_t(2),
    'paused': False,
    'auto_managed': True,
    'duplicate_is_error': True}
link = "magnet:?xt=urn:btih:4MR6HU7SIHXAXQQFXFJTNLTYSREDR5EI&tr=http://tracker.vodo.net:6970/announce"
handle = lt.add_magnet_uri(ses, link, params)
ses.start_dht()

print 'downloading metadata...'
while (not handle.has_metadata()):
    time.sleep(1)
print 'got metadata, starting torrent download...'
while (handle.status().state != lt.torrent_status.seeding):
    s = handle.status()
    state_str = ['queued', 'checking', 'downloading metadata', \
                'downloading', 'finished', 'seeding', 'allocating']
    print '%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s %.3' % \
                (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
                s.num_peers, state_str[s.state], s.total_download/1000000)
    time.sleep(5)

Which seems to work fine, but then slows down to less than a byte/s:

$ python test.py 
downloading metadata...
got metadata, starting torrent download...
0.00% complete (down: 195.0 kb/s up: 8.0 kB/s peers: 28) checking 3.069
0.00% complete (down: 133.0 kb/s up: 5.0 kB/s peers: 28) checking 3.342
0.00% complete (down: 29.0 kb/s up: 1.0 kB/s peers: 28) checking 3.359
0.00% complete (down: 5.0 kb/s up: 0.0 kB/s peers: 28) checking 3.398
0.00% complete (down: 4.0 kb/s up: 0.0 kB/s peers: 28) checking 3.401
0.00% complete (down: 0.0 kb/s up: 0.0 kB/s peers: 28) checking 3.405
0.00% complete (down: 0.0 kb/s up: 0.0 kB/s peers: 28) checking 3.408
0.00% complete (down: 0.0 kb/s up: 0.0 kB/s peers: 28) checking 3.412

It slows down and never completes. Any idea why this happens?

Edee answered 2/7, 2011 at 11:11 Comment(4)
And your torrenting client of choice works well with this particular torrent?Epiclesis
I tried several magnet-URIs, this is just the one found in #6052377 , all of the torrents I tried have more than enough peers and have downloaded with 200+ kb/s using another client.Edee
can you please help me out? I'm using the same code as your's but in my case it stuck in the loop while (not h.has_metadata()):. any though?Sirius
how to set, ses.listen_on(6881, 6891), how did you get 6881 and 6891Gassing
E
14

The problem turned out to be trivial. The save_path did not exist, thus the libtorrent library downloaded as long as it did not have to flush the cache, but once it attempted to write the file, it failed and could not continue downloading, therefore the slowdown and eventual halt. Once an existing path was added it worked fine.

Edee answered 3/8, 2011 at 17:25 Comment(0)
S
3

The download rate that you see is most likely from the actual metadata download (i.e. the .torrent file downloaded from your peers). Once the .torrent file has been downloaded, it's started. In this case.

It appears as if you already have some of the files that belong to this torrent, so the downloading is stopped and those files are being checked. i.e. the piece are read in, hashed and compared to the piece hashes in the .torrent file.

The last column shows you the number of megabytes found in the files that match the hashes, the last but one column show you the state of the torrent, i.e. checking.

If you wait until the checking is done, the download should resume.

Better yet, if you save resume data when you quit, and load it back in on startup, you don't have to re-check every time.

Svend answered 31/7, 2011 at 5:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.