Lesson map
Lesson 03 published. Map, example, EX007.
flowchart TD
N0["Learning outcomes"]
N1["3.1 Why security students program"]
N2["3.2 Data structures for networks"]
N3["3.3 Handling bad input"]
N4["3.4 Readable output"]
N5["Synthesis"]
N0 --> N1 --> N2 --> N3 --> N4 --> N5
N5 --> EX["Worked example"]
EX --> Q["Exercise EX007"]
Logical drawing
flowchart LR
N0["Learning outcomes"]
N1["3.1 Why security students program"]
N2["3.2 Data structures for networks"]
N3["3.3 Handling bad input"]
N4["3.4 Readable output"]
N5["Synthesis"]
N0 --> N1 --> N2 --> N3 --> N4 --> N5 --> Q["EX007"]
[Learning outcomes]
|
v
[3.1 Why security students program]
|
v
[3.2 Data structures for networks]
|
v
[3.3 Handling bad input]
|
v
[3.4 Readable output]
|
v
[Synthesis]
|
v
[Worked example] --> [Exercise EX007]
Learning outcomes
You will (1) write small stdlib Python scripts; (2) handle input carefully; (3) produce readable output.
3.1 Why security students program
Priya will analyse logs, check configurations and test rules, and doing these by hand is slow and error-prone. Short scripts make checks repeatable: the same script run next month gives comparable results. The course uses only the Python standard library so labs run anywhere without installing packages, and every lab uses synthetic data invented for teaching. Programming here is a tool for careful reasoning, not an end in itself, and the scripts are short enough to read in full, which matters because a script nobody understands cannot be trusted to support a conclusion.
3.2 Data structures for networks
Python's built-in structures map well to network data. A list holds rules in order, which matters because firewalls apply the first matching rule. A dictionary maps names to addresses or zones to subnets. A set holds unique items, such as the distinct devices seen in a log, and supports the union and intersection reasoning from chapter 1. A tuple holds a fixed record, such as source, destination and port. Choosing the right structure makes code simpler and errors less likely, for example using a set when duplicates must not be counted twice.
3.3 Handling bad input
Real data contains mistakes: truncated log lines, misspelled zone names, addresses with extra spaces. A script that crashes on the first bad line gives no result, and one that silently skips bad lines can give a misleading result. Priya learns to count and report malformed input separately, so a reader can see how much data was ignored. She also validates values, such as checking that a port is between 0 and 65535, before using them. Careful input handling is a security habit as well as a programming one, since attackers often rely on software mishandling unexpected input.
3.4 Readable output
Output should answer a question clearly. Priya's scripts print aligned columns with headers, end with a summary line, and state units such as percentages or counts. She avoids printing thousands of lines when a total and a short list of exceptions would do. Readable output helps the clinic's small IT team, who are not programmers, use the results. It also helps reviewers: a summary line such as "3 of 8 below minimum" can be checked against the listed rows, which makes errors in the script easier to notice.
Worked example
Priya writes a script that reads synthetic device records, skips and counts two malformed lines, and prints a table of devices by group with a final count.
Synthetic case. All data are synthetic.
Errors and limits
Scripts that silently drop bad input can produce confident but wrong summaries.
Exercises
- EX007 — Ordered rules structure. Which Python structure suits firewall rules, and why?
Show marking criteria
Rubric: 3 = list, because order decides first match;; 2 = list;; 1 = dictionary without reason;; 0 = none. - EX008 — Malformed line handling. How should a script treat malformed log lines?
Show marking criteria
Rubric: 3 = count and report them separately, not crash or silently skip;; 2 = skip them;; 1 = ignore;; 0 = none. - EX009 — Port validation. What range should a port value be checked against?
Show marking criteria
Rubric: 3 = 0 to 65535, rejecting values outside;; 2 = 0-65535;; 1 = up to 1024;; 0 = none.
Synthesis
Small, readable stdlib scripts make security checks repeatable and reviewable.
Continue studying — chapter 03 of 40
← Chapter 02 (draft) · Course chapter index · Chapter 04 (draft) →