Skip to content
Simple and functional BitTorrent client made in Python - Use for learning
Python
Branch: master
Clone or download
Latest commit 850cdd3 Aug 8, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore start upgrading to python3, need a better bencode lib Jun 2, 2019
README.md Update README.md Aug 8, 2019
block.py remake downloading logic Aug 7, 2019
main.py remake downloading logic Aug 7, 2019
message.py remake downloading logic Aug 7, 2019
peer.py typo Aug 7, 2019
peers_manager.py remake downloading logic Aug 7, 2019
piece.py remake downloading logic Aug 7, 2019
pieces_manager.py remake downloading logic Aug 7, 2019
rarest_piece.py start upgrading to python3, need a better bencode lib Jun 2, 2019
requirements.txt refactor peer fetching Jul 26, 2019
torrent.py remake downloading logic Aug 7, 2019
tracker.py

README.md

PyTorrent - Python BitTorrent

PyTorrent is a CLI tool that downloads files from the BitTorrent network.

I wanted to make my own functional and straightforward program to learn how does BitTorrent protocol work and improve my python skills.

It is almost written from scratch with python 3.7, only the pubsub library was used to create events when a new peer is connected, or when data is received from a peer. You first need to wait for the program to connect to some peers first, then it starts downloading.

This tool needs a lot of improvements, but it does its job, you can :

  • Read a torrent file
  • Scrape udp or http trackers
  • Connect to peers
  • Ask them for the blocks you want
  • Save a block in RAM, and when a piece is completed and checked, write the data into your hard drive
  • Deal with the one-file or multi-files torrents
  • Leech or Seed to other peers

But you can’t :

  • Download more than one torrent at a time
  • Benefit of a good algorithm to ask your peers for blocks (code of rarest piece algo is implemented but not used yet)
  • Pause and resume download

Don't hesitate to ask me questions if you need help, or send me a pull request for new features or improvements.

Installation

You can run the following command to install the dependencies using pip

pip install -r requirements.txt

💥 Because it's using the "select" function, this code will not be able to run on Windows: python-select-on-windows

Running the program

If you want to specify a torrent file, you need to edit it manually in the main.py file:

self.torrent = Torrent.Torrent("path_to_your_torrent") 

Then simply run: python main.py

The files will be downloaded in the same path as your main.py script.

Sources :

I wouldn't go that far without the help of Lita, PyTo for the message encoding/decoding logic, Kristen Widman's & the Bittorrent Unofficial Spec, so thank you.

You can’t perform that action at this time.