PyTorrent is a Python-based BitTorrent client with a web interface.
Instead of running everything in a terminal, you can upload a .torrent file in your browser, start the download, and watch what’s happening in real time.
This project was built as a CS50x Final Project and focuses on:
- Understanding how BitTorrent works internally
- Using
asynciofor real network-heavy tasks - Connecting a low-level backend with a clean web UI
PyTorrent has two main parts:
This part:
- Reads
.torrentfiles - Talks to trackers to find peers
- Connects to multiple peers at the same time
- Downloads files piece by piece
- Verifies each piece using SHA-1 hashing
This part:
- Lets users upload
.torrentfiles - Starts the download process
- Shows live logs and progress
- Keeps the app responsive while downloads run in the background
project/
│
├── app.py
│ → Flask web server and main entry point
│
├── torrent_wrapper.py
│ → Runs the async BitTorrent engine safely in the background
│
├── torrent_client/
│ ├── parser.py
│ │ → Decodes .torrent files (bencoding)
│ ├── get_peers.py
│ │ → Contacts trackers and finds peers
│ ├── connect_to_peer_async.py
│ │ → Handles peer connections and downloads
│ └── __init__.py
│
├── downloads/
│ → Final downloaded files
│
├── requirements.txt
│
└── README.md
This project follows the official BitTorrent specification (BEP 0003) instead of using libraries like libtorrent.
Each downloaded piece is verified using SHA-1 hashing.
If a piece is corrupted, it is discarded and downloaded again.
The dashboard uses Server-Sent Events (SSE) to stream logs and progress updates live to the browser.
Flask is synchronous, but BitTorrent is async.
A background thread runs an asyncio event loop so downloads don’t freeze the web server.
python3 -m venv venv
source venv/bin/activate # Mac/Linux
venv\Scripts\activate # Windowspip install Flask werkzeugpython3 app.pyThen open:
http://127.0.0.1:5000
- Open the dashboard in your browser
- Upload a
.torrentfile - Click Start Download
- Watch logs and progress update live
- Downloaded file appears in the
downloads/folder
PyTorrent helped me understand how real-world peer-to-peer systems work under the hood.
It combines networking, concurrency, cryptography, and web development into one complete system.