A simple P2P BitTorrent-like file sharing system implemented with HTTP protocol and Distributed Hash Table (DHT) support.
- Python 3: Core programming language
- Flask: Web framework for the metrics server
- HTTP Server: Native Python http.server for client and tracker communication
- Kademlia: Implementation of the Kademlia distributed hash table for P2P functionality
- TailwindCSS: Frontend styling for the metrics dashboard
Tool for generating torrent metadata files from source files.
Central coordinator that maintains a registry of which clients have which file chunks.
P2P client that downloads chunks from peers and shares them with others.
Distributed system for storing chunk location information, providing redundancy when the tracker is unavailable.
Dashboard for monitoring client activity, download/upload progress, and system health.
PUT /chunk
Request Body:
{
"client_host": "123.42.23.12",
"client_port": 8000,
"hashes": [
"oc3kikx1o9x1oxo21k1x",
"xyz123abc456def789",
...
]
}Response:
Ok
POST /chunk
Request Body:
[
"oc3kikx1o9x1oxo21k1x",
"xyz123abc456def789",
...
]Response:
[
{
"client_host": "123.42.23.12",
"client_port": 8000,
"hash_list": [
"oc3kikx1o9x1oxo21k1x",
...
]
},
{
"client_host": "189.45.67.89",
"client_port": 8001,
"hash_list": [
"xyz123abc456def789",
...
]
},
...
]POST /dht
Request Body:
{
"host": "123.42.23.12",
"port": 8468
}Response:
Ok
GET /dht
Response:
[
{
"host": "123.42.23.12",
"port": 8468
},
{
"host": "189.45.67.89",
"port": 8469
},
...
]POST /chunk
Request Body:
[
"oc3kikx1o9x1oxo21k1x",
"xyz123abc456def789",
...
]Response:
[
{
"hash": "oc3kikx1o9x1oxo21k1x",
"content": "base64_encoded_content_here"
},
{
"hash": "xyz123abc456def789",
"content": "base64_encoded_content_here"
},
...
]GET /ping
Response:
pong
The DHT implementation uses the Kademlia protocol with custom serialization for key-value storage:
- Keys: File chunk hashes (e.g., "oc3kikx1o9x1oxo21k1x")
- Values: JSON stringified arrays of client addresses (e.g.,
["123.42.23.12:8000", "189.45.67.89:8001"])
- Bootstrap: New nodes contact known peers (from tracker) to join the network
- Store: When a client has a chunk, it stores
chunk_hash -> ["ip:port"]in the DHT - Retrieve: When a client needs a chunk, it queries the DHT for
chunk_hashto get a list of peers - Update: When a client gets a new chunk, it adds itself to the list of providers for that chunk
The metrics server collects and displays real-time information about all active clients including:
- Download/upload progress
- Number of chunks downloaded/uploaded
- Total file size
- DHT status (enabled/disabled)
- Number of DHT peers
- Tracker connectivity status
- Upload/download ratio
Metrics are sent to the server every 3 seconds and displayed in a real-time dashboard at http://localhost:8080/.
pip install -r requirements.txtpython tracker.py [port_number]Default port is 5000 if not specified.
python torrentify.py <your_file> <tracker_ip:port>Example:
python torrentify.py example.mp4 localhost:5000This generates a torrent.json file containing metadata about the file chunks.
Use the tracker information that is output from the previous command.
python metric_server.pyAccess the dashboard at http://localhost:8080
To start a client with the source file (seeder):
python client.py has_file [dht_enabled]To start a client without the source file (leecher):
python client.py [dht_enabled]Add the dht_enabled parameter to enable DHT functionality incase when tracker is down.
python stress_test.pyThis will automatically start multiple clients and simulate a P2P network environment.
- client.py: Implementation of the torrent client
- dht.py: Kademlia-based distributed hash table implementation
- metric_server.py: Monitoring dashboard server
- torrentify.py: Utility to create torrent metadata files
- tracker.py: Central tracker server implementation
- util.py: Helper functions for network operations
- templates/dashboard.html: Metrics visualization dashboard
The system is designed with redundancy in mind:
- If the tracker is offline, clients can continue to discover peers through the DHT network
- If a peer disconnects, clients automatically find alternative sources
- Health checks remove unavailable peers from the tracker registry
This project is licensed under the terms of the license included in the LICENSE file.