Simple libtorrent Python client

假装没事ソ 提交于 2021-02-08 10:15:17

问题


I tried creating a simple libtorrent python client (for magnet uri), and I failed, the program never continues past the "downloading metadata". If you may help me write a simple client it would be amazing.

P.S. When I choose a save path, is the save path the folder which I want my data to be saved in? or the path for the data itself.

(I used a code someone posted here)

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)

回答1:


What happens it is that the first while loop becomes infinite because the state does not change.

You have to add a s = handle.status (); for having the metadata the status changes and the loop stops. Alternatively add the first while inside the other while so that the same will happen.




回答2:


Yes, the save path you specify is the one that the torrents will be downloaded to.

As for the metadata downloading part, I would add the following extensions first:

ses.add_extension(lt.create_metadata_plugin)
ses.add_extension(lt.create_ut_metadata_plugin)

Second, I would add a DHT bootstrap node:

ses.add_dht_router("router.bittorrent.com", 6881)

Finally, I would begin debugging the application by seeing if my network interface is binding or if any other errors come up (my experience with BitTorrent download problems, in general, is that they are network related). To get an idea of what's happening I would use libtorrent-rasterbar's alert system:

ses.set_alert_mask(lt.alert.category_t.all_categories)

And make a thread (with the following code) to collect the alerts and display them:

while True:
    ses.wait_for_alert(500)
    alert = lt_session.pop_alert()

    if not alert:
        continue

    print "[%s] %s" % (type(alert), alert.__str__())

Even with all this working correctly, make sure that torrent you are trying to download actually has peers. Even if there are a few peers, none may be configured correctly or support metadata exchange (exchanging metadata is not a standard BitTorrent feature). Try to load a torrent file (which doesn't require downloading metadata) and see if you can download successfully (to rule out some network issues).



来源:https://stackoverflow.com/questions/33694605/simple-libtorrent-python-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!