BitTorrent Client Documentation
Complete documentation for the Simple BitTorrent Client (fast-peer) implementation.
Installation & Requirements
This BitTorrent client requires Python 3.10+ and has minimal dependencies for maximum compatibility.
System Requirements
- Python 3.10+ (tested on Linux)
- requests library for HTTP tracker support
- libtorrent (optional, only for seeder.py)
Installation Steps
Project Structure
Command Reference
The BitTorrent client supports multiple commands for different operations. All commands are executed through the main.py file.
Basic Commands
Decode bencoded data and display as JSON.
Example:
Display detailed information about a torrent file.
Example:
Output includes: Tracker URL, file length, info hash, piece length, and piece hashes.
Discover and list available peers for a torrent.
Example:
Perform BitTorrent handshake with a specific peer.
Example:
Download a specific piece from a torrent.
Example:
Download the complete file from a torrent.
Example:
Magnet Link Commands
Parse and display information from a magnet link.
Example:
Perform handshake using magnet link information.
Retrieve and display metadata from magnet link.
Download a specific piece using magnet link.
Download complete file using magnet link.
Example:
DHT Commands
Use DHT for peer discovery and download files.
Features:
- Trackerless peer discovery
- DHT node bootstrapping
- Automatic peer replenishment
- Metadata exchange via DHT
Perform negotiated handshake with extended protocol support.
Core Classes
The BitTorrent client is built around several key classes that handle different aspects of the protocol.
Manages peer connections, DHT integration, and peer discovery strategies.
Key Responsibilities:
- Maintain active peer connections
- Integrate with DHT for peer discovery
- Handle peer connection lifecycle
- Manage unchoked peer list
Key Methods:
-
add_peer(peer_info)Add a new peer to the connection pool
-
get_best_peers(count)Get the best performing peers based on throughput
-
replenish_peers()Use DHT to find new peers when connections drop
-
cleanup_dead_peers()Remove disconnected or unresponsive peers
Handles piece downloading, verification, and distribution among peers.
Key Responsibilities:
- Track which pieces need to be downloaded
- Assign pieces to available peers
- Handle piece verification and retry logic
- Manage download priorities
Key Methods:
-
get_piece_for_peer(have_pieces)Get next piece to download based on peer's available pieces
-
mark_failed(idx)Mark a piece as failed and add back to needed pieces
-
get_size()Get number of remaining pieces to download
Implements BEP 5 DHT protocol for trackerless peer discovery.
Key Responsibilities:
- Bootstrap with known DHT nodes
- Handle DHT queries (ping, find_node, get_peers)
- Maintain routing table of known nodes
- Discover peers for specific info hashes
DHT Messages Supported:
-
pingBasic connectivity test between DHT nodes
-
find_nodeFind nodes closest to a target node ID
-
get_peersFind peers downloading/seeding a specific torrent
-
announce_peerAnnounce that this node is downloading/seeding
Bootstrap Nodes:
- router.bittorrent.com:6881
- dht.transmissionbt.com:6881
- router.utorrent.com:6881
- dht.aelitis.com:6881
- dht.libtorrent.org:25401
Manages torrent metadata exchange for magnet links.
Key Responsibilities:
- Download metadata pieces from peers
- Assemble complete metadata from pieces
- Handle metadata piece verification
- Retry failed metadata downloads
Key Methods:
-
get_next_missing()Get next metadata piece that needs to be downloaded
-
add_piece(index, data)Add a downloaded metadata piece
-
is_complete()Check if all metadata pieces have been downloaded
-
assemble()Assemble complete metadata from all pieces
Usage Examples
Basic File Download
Magnet Link Usage
DHT-based Download
Peer Discovery
Seeding (requires libtorrent)
Troubleshooting
Common Issues
Symptoms: Download fails with "no peers available"
Solutions:
- Check if torrent is still active/seeded
- Try using DHT command for trackerless discovery
- Verify network connectivity and firewall settings
- Check if tracker URLs are accessible
Symptoms: Downloads are slower than expected
Solutions:
- Increase number of concurrent connections
- Check network bandwidth and latency
- Try different torrents to isolate the issue
- Monitor peer connection quality
Symptoms: DHT commands fail or timeout
Solutions:
- Check UDP port 6881 is not blocked
- Verify internet connectivity
- Try different bootstrap nodes
- Check firewall/NAT configuration
Debug Mode
Enable verbose logging by modifying the log level in main.py:
Performance Tuning
- Concurrent connections: Adjust peer connection limits
- Piece size: Larger pieces = fewer requests but less granular progress
- Block size: Smaller blocks = more parallel downloads
- DHT refresh: More frequent DHT queries = better peer discovery