Set-up · 15 minutes
Set up and check your web lab
- Create the folder ~/krlab/web-apis for the whole course
- Check the tools that the lessons need with one script
- Read a failed check and fix its cause
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. Type the code yourself first: you learn more.
What you need
Every lesson of this course runs on your own computer. You need five things, and none of them costs money or needs an account.
| Need | Why |
|---|---|
| A Linux or macOS terminal | You type commands and run programs. |
| Python 3.12 or later | You build every server in Python, with the standard library only. |
curl | A command that sends an HTTP request and prints the answer. |
| A text editor | You write .py files. Any editor works. |
| Basic Python | You can read a function, an if, a dictionary and a class. Each new name is explained where it first appears. If Python is new to you, start with the course "Python for security work" on this site. |
You do not need sudo, a database server or an internet connection after the setup. Every server in this course listens on 127.0.0.1, the loopback address, so only your own computer can reach it. Never point a lesson at a system that is not yours.
- curl → Lab server: request to 127.0.0.1:8300
- Internet → Lab server (blocked): cannot connect
Set it up
1. Make the lab folder
mkdir (make directory) creates a folder. The option -p also creates the folders above it and gives no error if the folder exists. cd (change directory) moves you into it, and pwd (print working directory) shows where you are.
mkdir -p ~/krlab/web-apis
cd ~/krlab/web-apis
pwd/home/you/krlab/web-apisAll files of the course go into this folder.
2. Look at the two tools
python3 --version prints the version of Python. curl --version prints a long text, and head -n 1 keeps only the first line. The | sign gives the output of the first command to the second.
python3 --version
curl --version | head -n 1Python 3.14.4
curl 8.18.0 (x86_64-pc-linux-gnu) libcurl/8.18.0 OpenSSL/3.5.5 ...Your numbers differ, and the second line is shortened here. Python must be 3.12 or later (Python 3.10 no longer gets security fixes). If a command is not found, go to "If it fails".
3. Write the check script
The next step checks the same things in one place. Create labcheck.py in the lab folder. The script uses four parts of the Python standard library, the code that comes with Python:
sys.version_infoholds the version of the running Python.shutil.which("curl")searches yourPATH(the list of folders where the shell looks for programs) and returns the location ofcurl, orNone.importloads a module.http.serverruns a web server,sqlite3is a small database,secretsmakes random values that are safe for security work,hmaccompares secrets in constant time, andhashlibmakes hashes (lesson 3 uses itsscrypt).socketis the low-level network module. The script opens a socket and binds it to127.0.0.1and each port from 8300 to 8304, the ports of the lessons. If another program uses a port, the bind fails and the script names it.
labcheck.py
import hashlib
import shutil
import socket
import sys
passed = 0
def report(ok, text):
global passed
passed += ok
print(("ok " if ok else "FAIL ") + text)
version = sys.version_info
report(version >= (3, 12), f"python {version.major}.{version.minor}.{version.micro} (need 3.12 or later)")
report(shutil.which("curl") is not None, "curl is installed")
try:
import http.server, sqlite3, secrets, hmac
report(True, "modules http.server, sqlite3, secrets, hmac")
except ImportError as error:
report(False, f"a module is missing: {error}")
busy = []
for port in (8300, 8301, 8302, 8303, 8304):
try:
with socket.socket() as server:
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(("127.0.0.1", port))
except OSError:
busy.append(port)
report(not busy, "ports 8300 to 8304 are free on 127.0.0.1" if not busy else f"port in use: {busy}")
report(hasattr(hashlib, "scrypt"), "hashlib has scrypt (lesson 3)")
print(f"lab ready: {passed} of 5")The function report prints one line for each check and counts the passes. The last line shows how many of the five checks passed. Four Python habits in the script are new:
try: ... except ...:means "run this, and if it fails run that". Here a failed import or bind is reported instead of stopping the script.f"python {version.major}"is an f-string: the text inside{}is replaced by the value.passed += okaddsoktopassed. A true value counts as 1 and a false value as 0.global passedlets the function change the variablepassedthat lives outside it.
You may paste the script now and read it later. It only checks, and it changes nothing.
Check your lab
Run the script.
python3 labcheck.pyok python 3.14.4 (need 3.12 or later)
ok curl is installed
ok modules http.server, sqlite3, secrets, hmac
ok ports 8300 to 8304 are free on 127.0.0.1
ok hashlib has scrypt (lesson 3)
lab ready: 5 of 5Your Python version in the first line differs. The last line must be the same for every reader. If a line starts with FAIL, read "If it fails", fix it and run the script again.
Keep labcheck.py. Run it again whenever a lesson does not work, because it finds the most common causes in two seconds.
If it fails
| The line that fails | Cause | Fix |
|---|---|---|
python ... (need 3.12 or later) | Your system Python is old. | Install a newer Python with your package manager, or ask the owner of the machine. Then run python3.12 labcheck.py (use your version) and write python3.12 in every lesson. |
curl is installed | curl is missing or not in your PATH. | Install it with your package manager, for example sudo apt install curl on Debian or Ubuntu. |
modules ... | A Python build that leaves out a module, which is rare. | Install the full Python package of your system, not a minimal one. |
port in use: [...] | Another program already uses one of the listed ports, or a firewall tool blocks local sockets. | Stop the other program, for example a server from an earlier try (press Ctrl+C in its terminal). On Linux, ss -ltn lists the ports that are in use. On macOS, use lsof -iTCP -sTCP:LISTEN -n -P. |
hashlib has scrypt | Your Python was built without the OpenSSL scrypt function. | Install a Python that is built with a current OpenSSL. Only lesson 3 needs it. |
command not found: python3 | Python is not installed, or has another name. | Try python --version. If it shows 3.12 or later, use python instead of python3 everywhere. |
The path /home/you in this course is a stand-in for your own home directory. ~ always means your own home, so the commands work as they are.