Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Set-up · 15 minutes

Set up and check your web lab

After this lesson you can
  • 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
Lab files of this lesson (1)

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.

NeedWhy
A Linux or macOS terminalYou type commands and run programs.
Python 3.12 or laterYou build every server in Python, with the standard library only.
curlA command that sends an HTTP request and prints the answer.
A text editorYou write .py files. Any editor works.
Basic PythonYou 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.

Your lab stays on your computer
Other computersYour computerInternetcurlLab server12Other computersYour computerInternetcurlLab server12
  1. curl → Lab server: request to 127.0.0.1:8300
  2. 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-apis

All 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 1
Python 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_info holds the version of the running Python.
  • shutil.which("curl") searches your PATH (the list of folders where the shell looks for programs) and returns the location of curl, or None.
  • import loads a module. http.server runs a web server, sqlite3 is a small database, secrets makes random values that are safe for security work, hmac compares secrets in constant time, and hashlib makes hashes (lesson 3 uses its scrypt).
  • socket is the low-level network module. The script opens a socket and binds it to 127.0.0.1 and 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 += ok adds ok to passed. A true value counts as 1 and a false value as 0.
  • global passed lets the function change the variable passed that 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.py
ok    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 5

Your 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 failsCauseFix
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 installedcurl 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 scryptYour 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: python3Python 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.