Skip to content

A lightweight BitTorrent client built from scratch in Python to understand how the BitTorrent protocol works under the hood. This project focuses on learning-by-building — implementing key components of torrent communication, peer discovery, and data transfer.

Notifications You must be signed in to change notification settings

dvanhu/bittorrent-python

Repository files navigation

📦 BitTorrent Client in Python

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.

image

🚀 Overview

This project implements core components of a BitTorrent client from scratch, focusing on understanding the low-level mechanics of P2P networking.

It explains:

  • How .torrent files 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.


🔥 Features

Torrent Parsing

  • Decode .torrent files (bencoded data)
  • Extract info_hash, piece length, file metadata & trackers

Magnet Link Support

  • Parse magnet URIs
  • Extract info hash, display name, tracker list

Tracker Communication

  • HTTP/UDP tracker support
  • Announce to trackers & fetch peer lists

Peer Discovery + Handshake

  • Establish peer connections
  • Implement BitTorrent handshake
  • Extended handshake support

Piece Management (WIP)

  • Piece selection
  • Request/response pipeline
  • SHA-1 hash verification

🧠 Architecture (How It Works)

arch

1. Torrent / Magnet Parsing

File: torrent/parser.py
Extracts metadata required for downloading.

2. Tracker Communication

File: network/tracker.py
Sends announce requests and receives peer lists.

3. Client Engine

File: client/engine.py
Handles peer connections, handshakes, and piece workflow.

4. Entry Point

File: app/main.py
The CLI entrypoint connecting all modules.


🛠️ Installation

1. Clone the repository

git clone https://github.com/dvanhu/bittorrent-python.git
cd bittorrent-python

2. Install dependencies
pip install -r requirements.txt

About

A lightweight BitTorrent client built from scratch in Python to understand how the BitTorrent protocol works under the hood. This project focuses on learning-by-building — implementing key components of torrent communication, peer discovery, and data transfer.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published