1

我有个问题。我不是 C/C++ 程序员,libtorrent 文档对我来说并不是很清楚。没有像 libtorrent 的 python 文档那样可以找到的文档。

此刻,我尝试在 stackoverflow 中搜索代码示例,以了解如何将 save_state 和 load_state 用于会话。

谁能给我一个例子或解释我如何保存会话状态并稍后加载?

目标是在进程重新启动时恢复所有种子。

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

if os.path.isfile('./tempfile'):
    with open('./tempfile', 'wb+') as temp_file:
        ses.load_state(
            libtorrent.bdecode(temp_file.read())
        )
 params = {
            'save_path': '/home/downloads/',
            'storage_mode': libtorrent.storage_mode_t(2),
            'paused': False,
            'auto_managed': True,
            'duplicate_is_error': True
        }
        link = "magnet:?xt=urn:btih:4MR6HU7SIHXAXQQFXFJTNLTYSREDR5EI&tr=http://tracker.vodo.net:6970/announce"
        handle = libtorrent.add_magnet_uri(ses, link, params)
        while not handle.has_metadata():
            time.sleep(1)
with open('./tempfile', 'wb+') as temp_file:
    temp_file.write(libtorrent.bencode(ses.save_state()))

更新 当在 torrent 句柄上使用 save_state_resume() 时它也没有返回?我发现执行“pydoc libtorrent > libtorrentDoc.txt”给了我一个有用的文档来浏览和搜索。此时我将磁铁重新添加到会话中并使用上面的代码。

也许有人有更有效的方法?

4

1 回答 1

1

见:http ://www.libtorrent.org/reference-Session.html#save_state_flags_t

看起来 save_state 不打扰种子本身,但这是恢复会话状态本身的方法。

我不知道 Python,但我之前使用了一个名为 Luatorrent 的 Libtorrent 的 Lua 绑定,我只是将所有种子存储在一个表/数组中,然后在关闭时我会循环遍历表/数组并获取每个种子的暂停状态以及相关统计然后创建一个 save_resume_data() 文件停止种子然后结束会话。

使用我获得的数据,我将在 AppData 中存储一个 .dat 文件,该文件可以在下次客户端运行时再次加载,这将启动正在运行的种子并暂停已暂停的种子,它将具有完成百分比的数据等等

于 2015-02-21T14:13:24.313 回答