A BitTorrent client of your own

: the Networking board on two ways to send data, a postcard (UDP) and a phone call (TCP). Your client uses both. It sends postcards to a tracker and holds phone calls with peers.
Someone puts a film online. It is 129,302,391 bytes, about 129 MB. If 100,000 people want it and one server sends it to each of them, the server sends this much:
129,302,391 bytes × 100,000 people = 12,930,239,100,000 bytes
That is about 12.9 TB, and the server pays for all of it. The 100,000 is a teaching value, the file size is real. The people who downloaded the film already hold parts of it, and they are idle. BitTorrent lets them send those parts to each other.
By the end of this lesson you will have written a small BitTorrent client in Python. It reads a real .torrent file, asks a tracker for peers, talks to a stranger’s computer, downloads pieces of a real film, and proves each piece is correct before it keeps it.
The history (optional)
Bram Cohen designed BitTorrent in 2001 and wrote the first client in Python. According to Wikipedia and a TorrentFreak article, he announced the first release on a Yahoo! message board on 2 July 2001, and the announcement got one reply, which asked what BitTorrent was. He presented it at the first CodeCon, in San Francisco in February 2002. The film used here is Sintel, made by the Blender Foundation and put online in September 2010 under the Creative Commons Attribution 3.0 licence, so anyone may share it. Nothing in the lesson depends on this fold.
Before you start. You need four ideas from earlier lessons:
- A UDP packet is a small message that may be lost, and numbers inside it are written in network byte order (DNS 01 and 02: The UDP Socket and the Header built a 12-byte header this way, and DNS: Everything ran a whole server on it).
- TCP is a stream of bytes with no message boundaries, so a reader must count bytes itself (A TCP of your own over a TUN device).
- A hash turns any input into a short fixed-size fingerprint (Hashing).
- A swarm is a crowd of peers who all hold parts of one file, with no central copy (No centre at all: peer to peer, DHTs and Tor told the idea. This lesson builds it).