0%

Chapter 1

HTTP Is Text on a Socket

Welcome to the Course

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

At the end of Python in Practice, one call to requests.get sent something to the practice API and a and some came back. In Networks with Python you learned what travels underneath: bytes on a , arriving in pieces, framed so the reader knows where each message ends. This course puts the two together. , the protocol behind requests.get, turns out to be lines of text on exactly that kind of connection, and you will write both ends of it yourself.

What you will build

The course follows one project from start to finish.

ChaptersWhat you build
1 to 3wire.py, a small HTTP client: first a request written by hand on a , then requests built from any , then the standard library’s http.client doing the socket work
4 to 9a reading- server, a JSON API for books, that grows step by step: reading and writing books, redirects, a login, caching, and many requests on one connection
10 and 11a client for that server on requests and httpx, and the same server rewritten with a web framework,
12the capstone: the finished server and client, proven together

Each library arrives after you have written what it does by hand, so you can name the mechanism behind every feature you use.

What the site can and cannot reach

No lesson in this course contacts a real website. The sandboxes on Python Land have no route to the public internet, and nothing here pretends otherwise.

Your code talks to two kinds of server, and only these two:

  • practice-api, a small read-only HTTP service on Python Land’s internal network. It has no public address. Chapters 1 to 3 send it requests, and one lesson in Chapter 10 does too.

  • servers you write yourself. From Chapter 4 your own server listens only on the 127.0.0.1, the address a machine uses to talk to itself, and stops on purpose after a set number of connections.

The site also runs no encrypted connections. Real websites use HTTPS, which wraps HTTP in encryption; Chapter 9 explains what that adds and why the site cannot show it. Everything you see here is plain HTTP, so you can read every byte.

What this course needs from you

You need Networks with Python. The lessons expect that you can:

  • open a TCP connection with a timeout and send bytes with sendall;

  • keep a receive buffer, so bytes that arrive early wait for the next read;

  • read exactly n bytes with read_exact;

  • pass a a connect , so a test can hand it a fake socket instead of a network.

From Python in Practice you need one thing in particular: a single requests.get call with a timeout, whose status you checked and whose JSON you decoded. This course takes that call apart.

Python Fundamentals II is recommended, not required. From Chapter 4, some files the course supplies contain class lines and lines that start with @. The lessons explain what you need to read them, and no exercise asks you to write either one. If you want to understand those files fully, Fundamentals II teaches both.

Which servers will your code send requests to in this course?

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 plain words, what did a socket do for my programs in Networks with Python?

The answer is a useful reminder before the next lesson, which shows what an HTTP request looks like on that socket.

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.

When you have answered the question above, the next lesson opens an HTTP request and its reply as bytes and shows that both are made of lines.