【问题标题】:Downloading just the .torrent file from a magnet uri. Not sure what I'm actually downloading仅从磁铁 uri 下载 .torrent 文件。不确定我实际下载的是什么
【发布时间】:2019-08-08 07:06:30
【问题描述】:

给定一个磁铁文件,我正在尝试使用 Python 绑定 libtorrent 来获取一个 .torrent 文件。

#!/usr/bin/env python
import libtorrent as lt
import time
import sys
import random

ses = lt.session()
r = random.randrange(10000, 49000)
ses.listen_on(r, r+50)
print("Listening on ports %s - %s." % (r, r+50))
params = {
    'save_path': '.',
    'storage_mode': lt.storage_mode_t(2),
    'paused': False,
    'auto_managed': True,
    'duplicate_is_error': True,
    'file_priorities': [0]*5
    }

link = "magnet:?xt=urn:btih:209c8226b299b308beaf2b9cd3fb49212dbd13ec&dn=Tears+of+Steel&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Ftears-of-steel.torrent"

h = lt.add_magnet_uri(ses, link, params)
ses.add_extension('ut_metadata')
ses.add_extension('ut_pex')
ses.add_extension('metadata_transfer')
ses.add_dht_router("router.utorrent.com", 6881)
ses.add_dht_router("router.bittorrent.com", 6881)
ses.add_dht_router("dht.transmissionbt.com", 6881)
ses.add_dht_router("dht.aelitis.com", 6881)
ses.start_dht()
ses.start_lsd()
ses.start_upnp()
ses.start_natpmp()

while (not h.has_metadata()):
    time.sleep(1)
    status = ses.status()
    print("Seeking metadata for torrent (%s DHT nodes online)." %  status.dht_nodes)

torinfo = h.get_torrent_info()

torfile = lt.create_torrent(h.get_torrent_info())
f = open("torrentfile.torrent", "wb")
f.write(lt.bencode(torfile.generate()))
f.close()

几分钟后传输完成,我cat结果:

[me@localhost torrent]$ cat torrentfile.torrent 
d8:announce23:udp://explodie.org:696913:announce-listll23:udp://explodie.org:696934:udp://tracker.coppersurfer.tk:696931:udp://tracker.empire-js.us:133740:udp://tracker.leechers-paradise.org:696933:udp://tracker.opentrackr.org:133726:wss://tracker.btorrent.xyz25:wss://tracker.fastcast.nz32:wss://tracker.openwebtorrent.comee13:creation datei1552857262e4:info0:e[me@localhost torrent]$ 

预期的输出是一个二进制.torrent 文件,其中包含所有文件部分和哈希等。一些(可能)相关的系统信息:

[me@localhost torrent]$ python --version
Python 2.7.14
[me@localhost torrent]$ python -c "import libtorrent; print libtorrent.version"
1.0.10.0
[me@localhost torrent]$ uname -a
Linux ip-172-31-53-167.ec2.internal 4.14.104-95.84.amzn2.x86_64 #1 SMP Sat Mar 2 00:40:20 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

任何建议将不胜感激。我使用的示例代码实际上与声称可以为其他人工作的 sn-ps 相同。谢谢。

【问题讨论】:

    标签: python python-2.7 bittorrent libtorrent


    【解决方案1】:

    这看起来像是 1.0.10 中引入的 ABI 问题。

    如果您查看 1.0.10 的 changelog,它为编码条目引入了一种新类型 (preformatted)。这是为了在 torrent 文件中保留无效的密钥顺序(以允许对其重新编码并产生相同的信息哈希)。

    不幸的是,这破坏了之前 1.0.x 版本的 ABI。我在 RC_1_0 分支中 fixed 这个,在 1.0.12 中发布,但显然这从未发布过。

    简而言之,看起来您的 python 绑定库是使用 1.0.10 之前的版本构建的,但您的 libtorrent 库是 1.0.10 或更高版本。

    只要 python 绑定和主库来自同一版本的 libtorrent,你应该很好。

    【讨论】:

    • 谢谢,阿维德。我已经阅读了大量您的 Github 问题,试图解决这个问题(并试图在 CentOS 7 x64 上为 libtorrent 1.2 构建 Python 3.7 绑定但未成功。)我会再试一次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多