0%

Chapter 1

When the Server Has News

Welcome to the Course

Hi, I’m Erik. I write and maintain this course, along with the rest of Python Land.

In with Python a client always asked first, and your server only ever answered. That suits a reading , not a scoreboard, where the news happens on the server and every viewer wants it at once. This course builds the connection that lets a server speak first: a . You will run both ends of it yourself, inside one program, because nothing outside this site can reach your code.

What you will build

The course follows one project: a quiz-night scoreboard. Several viewers see every score change as soon as it happens, and one scorekeeper, who holds a secret token, is the only one allowed to change a score. Every message is a small piece of with a type, and the server’s messages also carry a version.

The course comes in two halves.

ChaptersWhat you build
1why WebSockets exist: what polling costs, and what two workarounds fix
2 to 5the , the that carry each message, and an echo service, written by hand with the standard library only
6 to 10the scoreboard on the websockets library: many viewers, slow and lost connections, a token check, then a tested server and client
11a taste of async: the same handler read and rewritten with async def
12where to go next

The code you write in Chapters 2 to 5 is a learning codec: it shows you every byte, so you know what a library does for you, and it is not meant to replace one. From Chapter 6 on you use the websockets package instead. It is already installed in your sandbox.

Where your code runs

No browser and no public address will ever connect to the code you write on this site. Your sandbox has no route to the public internet, and nothing outside it can reach in, so no lesson in this course contacts a real website.

Every live run happens in one place instead: inside one Python program, on the 127.0.0.1. That program starts the server and its clients, lets them talk, and stops on purpose after a set number of messages or a deadline. Real projects test WebSocket code the same way, because a test that needs a second machine or a browser is slow and fragile. When a lesson checks your work, it hands your stand-ins for connections and never opens a live one.

This chapter opens no connection at all; every number in it comes from a small simulation you can run in your browser.

Where will the WebSocket servers you write in this course accept connections?

What you bring

You need HTTP with Python and, through it, Networks with Python. The lessons expect that you can:

  • write an HTTP request and response as bytes, and parse a head with split;

  • read from a into a receive buffer, with read_exact for a known number of bytes and read_head for everything up to the blank line;

  • pack and unpack integers with struct in ;

  • run a server on 127.0.0.1 that stops on purpose, with a thread per connection;

  • test socket code by passing your functions a fake socket instead of a network;

  • use base64 and hashlib, as you did for Basic authentication and .

Python Fundamentals II is recommended, not required. Some supplied files contain class lines and lines that start with @; the lessons explain how to read them, and no exercise asks you to write either.

Meet Monty

Monty is Python Land’s learning guide, beside every lesson. Ask for a second explanation, a hint that does not hand you the answer, or help reading an error.

When you are signed in, your free account includes 5 Monty helps a day, shared across all courses. A good first question here:

In a few sentences, remind me what read_head did in HTTP with Python and why it kept the bytes after the blank line.

Chapter 2 leans on that function.

Guided gives you up to 50 Monty helps a day. Questions, hints, error explanations, extra practice, and code reviews share the same daily allowance. The counter shows what is left and when it resets. Every lesson and ordinary exercise retry remains free.

The next lesson tries the most obvious way to keep a scoreboard up to date with nothing but requests and responses, and measures what it costs.