Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 06 · From Scratch · DNS Server · about 16 min to read

Play it as a journeyTutor comes with a plan

A BitTorrent client of your own

Postcards and Phone Calls
Postcards and Phone Calls · Open board

: 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:

Lesson 44 of 117 in From Scratch

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • The torrent file: bencode, a format made of four shapes
  • The infohash: one name for the whole download
  • The tracker: asking one server for strangers
  • The handshake: 68 bytes to agree on the same download
  • The SHA-1 check: trust the list, not the stranger
  • bencode.py
  • tracker.py
  • wire.py
  • The run
  • What this client leaves out

Terms it explains: piece, bencode, infohash, tracker, handshake, bitfield.

Lab

The post loses one page in ten. What should you send again, and how much does the answer cost?

Drag Pages the post loses to 20%, then drag Pages allowed in the post at once and watch pages delivered per round trip.

Open The lost page

Related paper

Review From Scratch