A lightweight, educational BitTorrent client built entirely in Python — designed to help you understand the internals of the BitTorrent protocol: torrent parsing, peer discovery, tracker communication, handshakes, and piece exchange.
This project implements core components of a BitTorrent client from scratch, focusing on understanding the low-level mechanics of P2P networking.
It explains:
- How
.torrentfiles are structured - How magnet links work
- How peers and trackers communicate
- How the BitTorrent handshake works
- How pieces are exchanged between peers
Perfect for anyone exploring networking, protocol reverse engineering, or distributed systems.
- Decode
.torrentfiles (bencoded data) - Extract
info_hash, piece length, file metadata & trackers
- Parse magnet URIs
- Extract info hash, display name, tracker list
- HTTP/UDP tracker support
- Announce to trackers & fetch peer lists
- Establish peer connections
- Implement BitTorrent handshake
- Extended handshake support
- Piece selection
- Request/response pipeline
- SHA-1 hash verification
File: torrent/parser.py
Extracts metadata required for downloading.
File: network/tracker.py
Sends announce requests and receives peer lists.
File: client/engine.py
Handles peer connections, handshakes, and piece workflow.
File: app/main.py
The CLI entrypoint connecting all modules.
git clone https://github.com/dvanhu/bittorrent-python.git
cd bittorrent-python
2. Install dependencies
pip install -r requirements.txt