python - 仅从 Magnet URI 下载 .torrent 文件。不确定我实际上在下载什么

标签 python python-2.7 bittorrent libtorrent

给定一个 Magnet 文件,我尝试使用 libtorrent 的 Python 绑定(bind)来获取 .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

如有任何建议,我们将不胜感激。我使用的示例代码实际上与声称对其他人有用的代码片段相同。谢谢。

最佳答案

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

如果你看changelog对于 1.0.10,它引入了一种新的编码条目类型(预格式化)。这是为了在 torrent 文件中保留无效的 key 顺序(以允许重新编码并生成相同的信息哈希)。

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

简而言之,您的 python 绑定(bind)库似乎是使用 1.0.10 之前的版本构建的,但您的 libtorrent 库是 1.0.10 或更高版本。

只要 python 绑定(bind)和主库来自同一版本的 libtorrent,就应该没问题。

关于python - 仅从 Magnet URI 下载 .torrent 文件。不确定我实际上在下载什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55212467/

相关文章:

python - 获取 Instagram 访问 token

python - python中的这个程序如何工作?

python - 为 torrent 创建对等请求

linux - Planetlab 平台上的 Azureus/Vuze BitTorrent 客户端

python - 如何使用 python-libtorrent 获取 torrent 的对等列表?

python - 这个 python "for"复合语句是如何工作的?

python - 通过 lmfit 模型在 python 中最小化拟合两个洛伦兹

python - 将Python文件作为linux命令执行

python 2 : How do I condense like terms in a tuple?

python - 在 Python 中使用变量的问题