如何使用libtorrent for python来获取info_hash

use*_*033 4 python libtorrent

from libtorrent as lt
info = lt.torrent_info(open('example.torrent','rb').read())
info.info_hash()
Run Code Online (Sandbox Code Playgroud)

这不会得到哈希,而是得到了对象 <libtorrent.big_number object at ...... >

我该怎么办?

Zer*_*eus 7

现有的答案为您提供了所需的一切......但是这里有一些代码可以使它明确:

import libtorrent as lt
info = lt.torrent_info(open('example.torrent','rb').read())
info_hash = info.info_hash()
hexadecimal = str(info_hash)
integer = int(hexadecimal, 16)
Run Code Online (Sandbox Code Playgroud)

编辑:实际上,这是错的 - torrent_info()应该传递torrent文件的长度及其内容.修订(工作)版本:

import libtorrent as lt
torrent = open('example.torrent','rb').read()
info = lt.torrent_info(torrent, len(torrent))
info_hash = info.info_hash()
hexadecimal = str(info_hash)
integer = int(hexadecimal, 16)
Run Code Online (Sandbox Code Playgroud)

  • @ zero-piraeus如何解决unicode-decode错误?例如,我收到类似UnicodeDecodeError:'utf-8'编解码器的错误,无法解码字节0xc .....如何解决此问题? (2认同)