2016-05-15 5 views
0

トレントをダウンロードするためにPython-Libtorrentを使用しようとしています。私は予想通り急流を一時停止すると動作しない問題があります。ここではPython Libtorrent:トレントを予期しないように一時停止する

import libtorrent as lt 
import time 

import sys 


def get_libtorrent_session_and_start_dht(start_port, end_port): 
    ses = lt.session() 
    ses.listen_on(start_port, end_port) 
    ses.add_dht_router('dht.transmissionbt.com', start_port) 
    ses.add_dht_router('router.bittorrent.com', start_port) 
    ses.add_dht_router('router.utorrent.com', start_port) 
    ses.start_dht() 
    return ses 


const_state_str = ['queued', 'checking', 'downloading metadata', \ 
        'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume'] 
const_pause_torrent = "pause_torrent" 
const_resume_torrent = "resume_torrent" 
const_kill_torrent = "kill_torrent" 
const_user_logged_in = "user_logged_in" 
const_user_logged_out = "user_logged_out" 
const_quit_seeding = "quit_seeding" 

ses = get_libtorrent_session_and_start_dht(6881, 6891) 

torrent_info = None 
torrent_handle = None 
try: 
    torrent_info = lt.torrent_info("/home/horvste/Downloads/ubuntu-14.04.4-desktop-amd64.iso.torrent") 
    torrent_handle = ses.add_torrent(
     {'ti': torrent_info, 'save_path': "/home/horvste/Downloads"}) 
except Exception as exception: 
    raise exception 

while not torrent_handle.has_metadata(): 
    print "Getting meta data" 
    time.sleep(1) 

current_iteration = 0 
while not torrent_handle.is_seed(): 
    torrent_status = torrent_handle.status() 

    print "current_iteration: " + str(current_iteration) 

    if current_iteration == 10: 
     print "Calling torrent_handle.pause()...Pausing Torrent" 
     sys.stdout.flush() 
     torrent_handle.pause() 
     while not torrent_handle.status().paused: 
      print "Torrent Is Not Paused Yet" 
      time.sleep(1) 
      sys.stdout.flush() 
     while torrent_handle.status().paused: 
      print "Torrent Is Paused!" 
      torrent_handle.pause() 
      time.sleep(1) 
      sys.stdout.flush() 


    if const_state_str[torrent_status.state] == "checking": 
     print 'Checking Torrent....' 
     continue 

    print '\r%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \ 
      (torrent_status.progress * 100, torrent_status.download_rate/1000, torrent_status.upload_rate/1000, \ 
      torrent_status.num_peers, const_state_str[torrent_status.state]), \ 
     sys.stdout.flush() 

    current_iteration += 1 

    if torrent_status.paused: 
     print "Is Paused" 
    else: 
     print "Is Not Paused" 

    time.sleep(1) 

は、このスクリプトの出力です:

...Previous Output Checking Torrent 
Checking Torrent.... 
current_iteration: 0 
4.17% complete (down: 0.0 kb/s up: 0.0 kB/s peers: 0) downloading None 
Is Not Paused 
current_iteration: 1 
4.17% complete (down: 0.0 kb/s up: 0.0 kB/s peers: 0) downloading None 
Is Not Paused 
current_iteration: 2 
4.17% complete (down: 0.0 kb/s up: 0.0 kB/s peers: 4) downloading None 
Is Not Paused 
current_iteration: 3 
4.17% complete (down: 0.0 kb/s up: 0.0 kB/s peers: 7) downloading None 
Is Not Paused 
current_iteration: 4 
4.17% complete (down: 1.0 kb/s up: 1.0 kB/s peers: 11) downloading None 
Is Not Paused 
current_iteration: 5 
4.17% complete (down: 4.0 kb/s up: 2.0 kB/s peers: 16) downloading None 
Is Not Paused 
current_iteration: 6 
4.17% complete (down: 52.0 kb/s up: 5.0 kB/s peers: 21) downloading None 
Is Not Paused 
current_iteration: 7 
4.17% complete (down: 132.0 kb/s up: 8.0 kB/s peers: 28) downloading None 
Is Not Paused 
current_iteration: 8 
4.26% complete (down: 234.0 kb/s up: 12.0 kB/s peers: 33) downloading None 
Is Not Paused 
current_iteration: 9 
4.31% complete (down: 317.0 kb/s up: 15.0 kB/s peers: 38) downloading None 
Is Not Paused 
current_iteration: 10 
Calling torrent_handle.pause()...Pausing Torrent 
Torrent Is Paused! 
Torrent Is Paused! 
Torrent Is Paused! 
4.31% complete (down: 418.0 kb/s up: 19.0 kB/s peers: 46) downloading None 
Is Not Paused 
current_iteration: 11 
4.44% complete (down: 0.0 kb/s up: 0.0 kB/s peers: 10) downloading None 
Is Not Paused 
current_iteration: 12 
4.44% complete (down: 0.0 kb/s up: 1.0 kB/s peers: 12) downloading None 
Is Not Paused 
current_iteration: 13 
4.44% complete (down: 1.0 kb/s up: 2.0 kB/s peers: 21) downloading None 
...Torrent keeps downloading 

あなたは上記のスクリプトから見ることができるように、私は急流に一時停止を呼び出しますループの3回の反復で一時停止し、再度ダウンロードを開始します。以前のスクリプトは、このwhileループでtorrent_handle.pause()コール持っていなかったことに注意することが重要である:私はまだ同じ出力を取得しています

while torrent_handle.status().paused: 
    print "Torrent Is Paused!" 
    torrent_handle.pause() 
    time.sleep(1) 
    sys.stdout.flush() 

を。急流は予想どおり中断しません。私はUbuntu 14.04.4を実行していて、すべてがapt-getでインストールされています。 libtorrentのinit.pyの私のバージョン番号はversion = '0.16.13.0'です。私は何かを見逃したり、図書館を悪用していますか?

+0

「NOTE」torrent_handle ::一時停止のドキュメント内のボックスがあります() – Arvid

答えて

0

急流が自動デフォルトで管理され、それがadd_torrent_paramsのフラグを、一時停止のうち、それらをもたらすチェックすることができ

関連する問題