4
votes
from libtorrent as lt
info = lt.torrent_info(open('example.torrent','rb').read())
info.info_hash()

This doesn't get the hash, instead I get the object <libtorrent.big_number object at ...... >

What should I do?

3
Please give more information about what you tried - otherwise this question will be closed.Ramchandra Apte
info_hash() returns an object. There is most likely a method to access the hash in a hexadecimal form.Ramchandra Apte

3 Answers

7
votes

The existing answers give you everything you need ... but here's some code to make it explicit:

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)

EDIT: Actually, that's wrong - torrent_info() should be passed the length of the torrent file as well as its content. Revised (working) version:

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)
0
votes

According to http://www.rasterbar.com/products/libtorrent/manual.html#torrent-info

info_hash() returns the 20-bytes sha1-hash for the info-section of the torrent file. 
For more information on the sha1_hash, see the [big_number] class.

So http://www.rasterbar.com/products/libtorrent/manual.html#big-number

Just iterate over the bytes and you have your hash.

-2
votes

Just call str(info.info_hash()).

Edit: actually str is not correct. But what be the correct way to write out the hex string?