0%

Capstone: The Reading List Service and Client

Take It Home

This lesson has no Check my work. The site can see the project you saved here, and nothing that happens on your own computer. The run below, your server in one and your client in another, is the run the site could never show you, and it is yours alone. The quizzes ask why each step is there, and passing them completes the lesson.

What travels

Open README.md in the Project tab and read it now. It is the page someone else would read first: what the two programs are, how to set them up once, how to run them in two terminals, what the client’s exit codes mean, and what does not travel. It was written for this project and you can edit it; nothing checks it.

When you hand the project to someone, or to yourself on another computer, these files go:

  • the code: reading_server.py, reading_client.py, http_adapter.py, fakes.py, and wire.py, the hand-written client of the early chapters;

  • the tests in tests/;

  • SERVICE_CONTRACT.md, the agreement both programs follow;

  • README.md and requirements.txt.

Some things never travel: .venv, __pycache__, and .pytest_cache, because each computer makes its own from the recipe and the code, as Python in Practice showed. reading_cache.json stays behind too. show writes it in whatever folder the client runs in, it holds your own copies of replies from your own server, and it is safe to delete. pl_http.py played the second terminal on the site; if your download includes it, you will not need it.

Which of these belongs in the folder you hand to someone else?

Open requirements.txt. It pins two packages: requests==2.34.2, which the client imports, and pytest==9.1.1, which runs the tests. The server needs nothing beyond the standard library. On the site both came with the lesson’s Python; on your computer you install them yourself.

Download and rebuild

Use Download project in the project toolbar while you are signed in, and extract the archive into a new, empty folder. Open a terminal, change into that folder with cd, and build the environment there. The commands are the ones in README.md. They call the ’s own Python directly, so nothing needs activating:

python3 -m venv .venv
.venv/bin/python -m pip install -r requirements.txt
.venv/bin/python -m pytest -q

The tests that passed on the site should pass here too: they need no network and no server, which is what Lesson 4 made sure of. The project needs Python 3.10 or newer, as README.md says.

The commands are the ones the course has run. The Windows and tabs follow README.md and have not yet been run on those systems. On Windows, if py is not found, use python in its place.

Two terminals

In the first terminal, in the project folder, start the server. Its first line is listening on 127.0.0.1: and a , and then it waits:

.venv/bin/python reading_server.py

The server handles six connections and then stops by itself. It waits at most 30 seconds for each one, and a wait that ends with no client still uses up one of the six, so keep the second terminal ready. --max-connections (1 to 10) and --idle-timeout (up to 60 seconds) change both, as README.md says.

Open a second terminal, change into the same folder, and run the seven client commands from README.md, with the number from the server’s first line in place of PORT:

.venv/bin/python reading_client.py list --base-url http://127.0.0.1:PORT
.venv/bin/python reading_client.py add --base-url http://127.0.0.1:PORT --token tok-ana-writes-7Qx2 --title Piranesi --author "Susanna Clarke"
.venv/bin/python reading_client.py show 3 --base-url http://127.0.0.1:PORT
.venv/bin/python reading_client.py show 3 --base-url http://127.0.0.1:PORT
.venv/bin/python reading_client.py update 3 --base-url http://127.0.0.1:PORT/v0 --token tok-ana-writes-7Qx2 --title "Piranesi (paperback)" --author "Susanna Clarke"
.venv/bin/python reading_client.py delete 3 --base-url http://127.0.0.1:PORT --token tok-ana-writes-7Qx2
.venv/bin/python reading_client.py list --base-url http://127.0.0.1:PORT

When the course’s own version of the project ran these on a Mac, every command exited with 0. The update went through the old path: it printed followed 308 from http://127.0.0.1: with the port and /v0/books/3 on standard error, and its result on standard output. The second show sent nothing. After the last list, the server printed served its connections and stopped and exited, having served 7 requests on 6 connections, and reading_cache.json was left in the folder.

Seven client commands, and the server, which stops after six connections, stopped right after the seventh command. Why?

What the site cannot see

Nothing from this run comes back to Python Land. There is no token to paste and no check to pass, and no lesson asks for a result. The books you added lived in your server’s memory and were gone when it stopped.

The server listens on 127.0.0.1 only. Every request between your two programs stays inside your computer, on the , so no other machine can connect to it, and you never need to open a firewall for it.

After the two-terminal run, what does Python Land know about it?

You now own a tested service and client, and you know what each layer under them does. The last chapter names where that knowledge leads.