btinfohash – A Small Python Utility for Getting an info_hash

While attempting to add torrents to a closed tracker I needed the info_hash structure for a given torrent file, as this is what the tracker uses to identify a given torrent file. I tried searching the regular sources but came up empty (I later discovered that btshowmetainfo from the original python bittorrent client package also shows the calculated info_hash for a torrent file), so I wrote a simple python script for all your shell scripting or command line needs.

import bencode, BTL, hashlib, sys

if (len(sys.argv) < 2):
    sys.stderr.write("Usage: " + sys.argv[0] + " \n")

    with open(sys.argv[1], "rb") as torrent_file:
            bstruct = bencode.bdecode(

            if 'info' in bstruct:
                sys.stderr.write("Did not find an info element in the torrent file.\n")
        except BTL.BTFailure:
          sys.stderr.write("Torrent file did not contain a valid bencoded structure.\n")
except IOError as e:
    sys.stderr.write("Could not open file: " + str(e) + "\n")

This depends on the and from the bencode python package (their .eggs are borked, so you’ll have to copy the two .py files yourself after downloading the source file).

One thought on “btinfohash – A Small Python Utility for Getting an info_hash”

Leave a Reply

Your email address will not be published. Required fields are marked *