【问题标题】:How to download specific files by using python-libtorrent如何使用 python-libtorrent 下载特定文件
【发布时间】:2013-04-27 06:36:28
【问题描述】:

当我使用 python-libtorrent 实现客户端时 我在 GitHub 上找到了一个示例

import libtorrent as lt
import time

ses = lt.session()
ses.listen_on(6881, 6891)


e = lt.bdecode(open("test.torrent", 'rb').read())
info = lt.torrent_info(e)

h = ses.add_torrent(info, "d:\\temp")

while (not h.is_seed()):
    s = h.status()

    state_str = ['queued', 'checking', 'downloading metadata', \
    'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
    print s.download_rate
    print '%.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])

    time.sleep(1)

它工作正常。但它会下载所有的包含文件

我想要求用户选择文件,这个客户端应该只下载特定的文件。

怎么做?谢谢。

【问题讨论】:

标签: python libtorrent


【解决方案1】:

首先,您需要找出哪些片段属于所选文件。 为此,您首先需要找出文件的索引。 这样做就像通过 info.files() 一样简单

i=0
for f in info.files():
    if fileIndex == i:
        fileStr = f
        break
    i += 1

您可以通过打印其路径来确认这是正确的文件:

print fileStr.path

现在你需要找到文件到片段的映射并分配优先级(0表示不下载)

h = ses.add_torrent(info, "d:\\temp")

pr = info.map_file(fileIndex,0,fileStr.size)
n_pieces = pr.length / info.piece_length() + 1 

for i in range(info.num_pieces()):
    if i in range(pr.piece,pr.piece+n_pieces):
        h.piece_priority(i,7)
    else:
        h.piece_priority(i,0)

现在您可以像以前一样下载文件了。请记住,您现在需要通过检查进度来停止下载,因为您没有像以前那样进入种子模式:

while (not h.is_seed()):
    s = h.status()

    state_str = ['queued', 'checking', 'downloading metadata', \
    'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
    print s.download_rate
    print '%.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])

    if s.progress>=1:
        break

    time.sleep(1)

【讨论】:

    【解决方案2】:

    有更简单的方法 torrent_handle.prioritize_files 或 torrent_handle.file_priority

    参见 libtorrent 参考: http://www.rasterbar.com/products/libtorrent/manual.html#torrent-handle

    【讨论】:

    • prioritze_files 不存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多