2011-07-02 111 views
14

我有以下Python代碼:下載與libtorrent-python的一個Torrent中

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) 

這似乎是做工精細,但隨後緩慢下降到不足一個字節/秒:

$ 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 

它放慢速度,永遠不會完成。任何想法爲什麼發生這種情況

+0

而你選擇的torrent軟件可以和這個特殊的torrent一起使用嗎? – Gleno

+0

我嘗試了幾個磁鐵URIs,這只是在http://stackoverflow.com/questions/6051877/loading-magnet-link-using-rasterbar-libtorrent-in-python中找到的,我嘗試過的所有種子都有超過了足夠的同行,並使用另一個客戶端下載了200 kb以上的文件。 – cdecker

+0

你能幫我解決嗎?我使用相同的代碼作爲你的,但在我的情況下,它卡在循環while(而不是h.has_metadata()):'。任何雖然? – muaaz

回答

13

問題原來是微不足道的。 save_path不存在,因此libtorrent庫只要不需要刷新緩存即可下載,但一旦它嘗試寫入文件,它就會失敗並無法繼續下載,因此速度變慢並最終停止。一旦添加了現有的路徑,它就可以正常工作。

3

您看到的下載速度很可能來自實際的元數據下載(即從同伴下載的.torrent文件)。一旦.torrent文件被下載,它就開始了。在這種情況下。

它看起來好像你已經有一些屬於這個torrent的文件,所以停止下載並且正在檢查這些文件。即片段被讀入,散列,並與.torrent文件中的片段散列進行比較。

最後一列顯示與哈希匹配的文件中找到的兆字節數,最後一列顯示了該種子的狀態,即檢查。

如果您等待檢查完成,則應繼續下載。

更好的是,如果您在退出時保存恢復數據,並在啓動時加載它,則不必每次都重新檢查。

相關問題