A Python-based peer-to-peer file sharing system that uses a central registry server for peer discovery and parallel byte-range downloads between peers.
This project demonstrates a simple P2P file sharing architecture. A central server keeps track of active peers and the files they provide, while each peer can both serve local files and download missing files from other peers. Downloads are split into byte ranges so multiple providers can transfer different parts of the same file concurrently.
- Central server for peer registration, provider tracking, and file search.
- Peer nodes that serve local repository files over TCP sockets.
- Parallel downloads from multiple peers using byte-range partitioning.
- Schedule-driven download jobs with configurable startup wait time.
- Test data generation for a three-peer demo environment.
- Lightweight implementation using only the Python standard library.
.
├── P2PFileSharingServer.py # Central registry server entry point
├── P2PFileSharingPeer.py # Peer node entry point
├── common/
│ └── protocol.py # Shared socket protocol helpers
├── server/
│ ├── file_search_server.py # Central server socket loop
│ ├── peer_registry.py # Active peer and file provider registry
│ └── request_handler.py # Server-side command handling
├── peer/
│ ├── file_provider.py # Peer file serving socket
│ ├── file_downloader.py # Parallel download logic
│ ├── peer_server.py # Peer lifecycle orchestration
│ └── schedule_parser.py # Schedule file parser
├── example/ # Example peer schedules and setup notes
├── create_test_data.py # Generates demo peer repositories
├── start_p2p_tmux.sh # Optional tmux launcher
└── run_all.bat # Optional Windows launcher
- Python 3.9 or newer recommended.
- No third-party Python packages are required.
python3 create_test_data.pyThis creates three local repositories:
peer1-repopeer2-repopeer3-repo
Each repository contains sample .dat files used by the demo schedules.
Open a terminal and run:
python3 P2PFileSharingServer.py 5001Open three additional terminals and run one peer in each terminal:
python3 P2PFileSharingPeer.py 127.0.0.1:5001 peer1-repo example/peer1-schedule.txt 6001python3 P2PFileSharingPeer.py 127.0.0.1:5001 peer2-repo example/peer2-schedule.txt 6002python3 P2PFileSharingPeer.py 127.0.0.1:5001 peer3-repo example/peer3-schedule.txt 6003python3 P2PFileSharingServer.py <Port>Example:
python3 P2PFileSharingServer.py 5001python3 P2PFileSharingPeer.py <ServerIP:Port> <RepoDir> <ScheduleFile> <PeerPort>Example:
python3 P2PFileSharingPeer.py 127.0.0.1:5001 peer1-repo example/peer1-schedule.txt 6001Schedule files define when a peer starts and which files it should download.
wait 500
e.dat:00104857600
f.dat:00209715200
wait <milliseconds>delays the peer before it starts processing download jobs.<filename>:<size>requests a file and provides the expected file size in bytes.
- The central server starts and waits for peer connections.
- Each peer starts its own file provider server on the configured peer port.
- Each peer registers itself with the central server using
START SERVING. - Each peer sends its available file list using
START PROVIDING. - When a schedule requests a file, the peer searches the central server using
START SEARCH. - The central server returns available providers.
- The downloading peer splits the target file into byte ranges and downloads parts in parallel from providers.
- After a successful download, the peer registers the newly downloaded file as available for other peers.
download.log: records which providers were used for downloaded file parts.done: created when a peer completes all scheduled downloads successfully.
- Run all commands from the project root directory.
- Make sure each peer uses a unique port.
- If a port is already in use, stop the previous process or choose a different port.
- The included demo files are large sparse files, so their apparent size may be larger than the actual disk space used depending on the operating system.
No license has been specified for this repository.