pythonlibtorrent使用magnet lin创建空torrent

2024-09-28 03:12:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图下载一个torrent(特定的.torrent文件),只提供一个info_散列。我知道这之前在这里讨论过,我甚至搜索并修改了我的代码。结果如下:

import libtorrent as lt
import time
import sys
import bencode

ses = lt.session()
ses.listen_on(6881, 6891)
params = {
    'save_path': '.',
    'storage_mode': lt.storage_mode_t(2),
    'paused': False,
    'auto_managed': True,
    'duplicate_is_error': True
    }

info_hash = "2B3AF3B4977EB5485D39F96FE414729530F48386"
link = "magnet:?xt=urn:btih:" + info_hash

h = lt.add_magnet_uri(ses, link, params)

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.start_dht()

while (not h.has_metadata()):
    time.sleep(1)

torinfo = h.get_torrent_info()

fs = lt.file_storage()
for f in torinfo.files():
  fs.add_file(f)
torfile = lt.create_torrent(fs)
torfile.set_comment(torinfo.comment())
torfile.set_creator(torinfo.creator())

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

这将生成一个无法通过传输加载的torrent文件。它缺少跟踪器和真实的片段(创建\x00而不是实际片段)。
以下行保存碎片,但仍缺少跟踪器,无法通过传输打开:

^{pr2}$

我怎样才能创建一个看起来像真正的torrent的torrent,只需使用magnet链接(如代码中所述)?
(我使用的是Ubuntu15.04 x64和libtorrent 0.16.18-1)

我不是非法下载的文件背后的torrent-但是,我有一个torrent比较我的脚本下载的torrent。在


Tags: 文件importltinfocomaddstoragefs
1条回答
网友
1楼 · 发布于 2024-09-28 03:12:23

您没有设置片段散列和片段大小(属于file_storage对象)。参见documentation。在

然而,创建.torrent文件的一种更简单、更健壮的方法是使用create_torrent构造函数,它直接接受torrent_info对象。i、 电子邮箱:

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

相关问题 更多 >

    热门问题