← Back to all lessons

01 / Foundations / 30 min READ + PRACTICE

Python for security experiments.

Practice byte strings, files, exceptions, timeouts, logging, virtual environments and tests.

Practice boundaryUse your own devices, your isolated practice VM or 127.0.0.1. Get explicit authorization for any other system.

Bytes are the wire format

A network socket sends bytes, not Python strings. Encode explicitly as UTF-8 when constructing requests and decode received data only when the protocol defines an encoding. Hex output helps when data is not text.

Make failure visible

Use bounded timeouts and catch specific errors. Log host, port and a concise failure reason without recording credentials or raw session cookies. Prefer context managers for files and sockets so resources close even when an experiment fails.

LAB TERMINAL / EXAMPLE
payload = 'hello lab'.encode('utf-8')
print(payload.hex())
print(bytes.fromhex(payload.hex()).decode('utf-8'))

Build in a virtual environment

Keep each experiment in its own folder with README.md, requirements.txt and tests. Work from the standard library first; add dependencies only when their benefit is clear. Never commit real tokens or private traffic captures.

HANDS-ON EXERCISE

Try it in your lab

Write a function that converts a UTF-8 message into hexadecimal and back; test an accented word and an invalid byte sequence.

CHECKPOINT

Make sure it clicks

What happens when a socket recv() call returns b""?

TRY WITH OPENCODE

A prompt you can adapt

Write pytest tests for my Python bytes-to-text helper, including invalid UTF-8 input and a documented error message.
How to set up OpenCode ↗
Your progress stays in this browser only.