Home > python > Decode .torrent files

Decode .torrent files

Yesterday I tried Tribler, which is a next generation torrent client. Its novelty is that it is decentralized, thus it doesn’t need trackers at all.

I noticed that Tribler contacts other clients to collect .torrent files that are stored locally. This way every client becomes a small “tracker”. I got curious to see what those .torrent files are. How to decode them?

.torrent files have a so-called bencode format. At effbot I found a simple decoder in Python that I extended a little bit. My version can be found here (and it’s part of my jabbapylib library now).


./bdecode.py ubuntu.torrent


{   'announce': 'http://torrent.ubuntu.com:6969/announce',
    'announce-list': [   ['http://torrent.ubuntu.com:6969/announce'],
    'comment': 'Ubuntu CD releases.ubuntu.com',
    'creation date': 1318500053,
    'info': {   'length': 731164672,
                'name': 'ubuntu-11.10-desktop-amd64.iso',
                'piece length': 524288}}

697.3 MB

As you can see, information about pieces is omitted. If you need that too, use the variable torrent.long_torrent.

Update (20141228)
If you want a Python 3 solution, check out the project BencodePy.

Categories: python Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: