…
…
Parse a website's daily access log line by line with a regular expression; filter out broken lines and find the status code distribution, the slowest requests, hourly traffic, the paths with the most 404s, and suspicious IPs that send many requests per minute.
Campus Notes is a small site where university students share their course notes. On the Saturday of exam week (November 14, 2026), users complained that the site had slowed down, and the administrator noticed strange traffic around midday. The only thing available is the server's access log for that day: access.log, which writes each request as one line.
Data: access.log has 248 lines. Each line has the IP address that made the request (local addresses in the 10.x and 192.168.x format), the date and time, the method (GET, POST), the path, the status code, and the response time:
Output10.0.3.51 - [2026-11-14 07:13:45] "GET /notes/physics-1 HTTP/1.1" 200 121msAs in real logs, a few lines are broken: a line cut off halfway, a restart note from the server, a missing field, a different date format. The values are sample data.
What you will have at the end: A parser that turns every valid line in the log into a dictionary, and the answers to these questions: how many lines are broken, what the error rate is, which requests are the slowest, how the traffic spreads over the hours, which paths get 404s, and who is scanning the site:
OutputThreshold 10:
192.168.7.66 13:42 18 requestsLessons used: string methods (21), aligned reports with f-strings (22), enumerate, sorting and slices (24), dictionaries and lists of records (26–28), tuples and sets (29), list comprehensions (30), sorting with lambda (31), reading a text file (33), datetime and Counter (37), regular expressions and groups (38).
How to work: The steps share one session; lines, read in step 1, and records, built in step 3, are used in the later steps. If you change a cell, run the cells after it again. The check also tests your functions (parse_line, suspicious_ips) with small test data next to the lines in the file.
What you'll do
access.logDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Campus Notes is a small site where students share their course notes. The server writes every incoming request as one line in the access.log file. On a Saturday of exam week (November 14, 2026) the site slowed down, and the administrator also noticed strange traffic. Your task is to analyze this day's records and find out what happened.
A line looks like this:
Output10.0.3.51 - [2026-11-14 07:13:45] "GET /notes/physics-1 HTTP/1.1" 200 121msIn order: the IP address of the computer that made the request, the date and time, the method (GET asks for a page, POST submits a form), the requested path, the status code (200 OK, 404 not found, 500 server error), and the server's response time (milliseconds).
The first job is to read the file line by line into a list (lesson 33). Clean the \n at the end of each line with strip(); otherwise the pattern in step 2 will not match any line.
This step's code cell is for members.Sign up free
Splitting the line with split(" ")
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Each piece of the pattern matches one field in the line; you put the pieces you want to separate in parentheses to make a group:
| Field | Pattern piece |
|---|---|
| IP | (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) |
| Date and time | \[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\] |
| Method and path | inside quotes: the method from capital letters ([A-Z]+), one space, the path (\S+), one space, and HTTP/1\.1 |
| Status and duration | a three-digit status code, one space, one or more digits, and ms right after |
Square brackets and the decimal point have a special meaning in a pattern; to search for the characters themselves, put \ before them. \S+ means "one or more characters that are not spaces", which suits a path.
parse_line(line) returns a dictionary if the line matches the pattern completely (re.fullmatch), and None if it does not. In the dictionary, time should be a datetime object (lesson 37), and status and duration should be integers; in the next steps we will read hours and compare durations.
This step's code cell is for members.Sign up free
Now run all 248 lines through parse_line. Lines that match go to the records list, and lines that do not go to the bad_lines list. Before throwing a bad line away, it is a good habit to note it with its number: a cut-off line, a restart note, or a different date format in server logs is often the trace of another problem.
Put (line number, line) tuples in the bad_lines list; numbers start at 1 (enumerate(..., start=1), lesson 24). An empty line also counts as bad; to make it visible on screen, we print the repr form ('...').
This step's code cell is for members.Sign up free
The first question: how successful are the requests? Count how many times each status code appears with Counter (lesson 37) and print the codes from smallest to largest, with their percentages.
Status codes fall into groups: 2xx successful, 3xx redirect or "not modified, use the cached copy", 4xx the client's mistake (a page that does not exist), 5xx the server's mistake. The share of codes 400 and above in the total is the error rate; it is the first number the administrator looks at. Write it as a function: error_rate(statuses) takes the counter and returns the percentage; it also works with the counter of another day.
This step's code cell is for members.Sign up free
Users say "the site is slow". Which requests are slow? Sort the records by duration from largest to smallest and take the first five (lessons 24 and 31: sorted, key=lambda, reverse=True, slice).
When printing the time, use strftime("%H:%M") (lesson 37); the full date is not needed because everything is on the same day.
Looking at the output, something will catch your eye: most of the slow requests come from the same few paths. The administrator should look at those paths first.
This step's code cell is for members.Sign up free
How is the traffic spread over the day? Count the hour of each record (time.hour) with Counter and print one line for each hour from the first hour to the last. If an hour had no requests at all, we would want to show it as 0; that is why we go through the range(first, last + 1) and not the keys of the counter. Counter returns 0 for a key that is not there.
Next to the numbers, draw a simple bar: one # for every 2 requests ("#" * (count // 2)). This bar chart drawn with text shows the busiest hours at a glance. At the end, print the busiest hour with most_common(1).
Busy evening hours are expected in exam week. But you will see an unexpected jump in one of the midday hours; you will find the reason in step 8.
This step's code cell is for members.Sign up free
404 means "not found". If a path gets many 404s, there are two possibilities: there is a broken link on the site (a link still points to a deleted notes page), or someone is trying pages that are not on the site. The administrator needs to know both.
Count the paths of only the records whose status is 404 and print the 5 most common paths with most_common. Also say how many different paths got a 404; you can turn them into a set and look at its length (lesson 29), or the length of the counter gives the same thing.
This step's code cell is for members.Sign up free
A person browsing a site opens a few pages per minute. If dozens of requests come from the same IP within one minute, a program (a bot) is most likely scanning the site: trying admin pages that do not exist and password files. The strange paths you saw in the 404 list in the previous step may be because of this.
suspicious_ips(records, threshold) counts the requests in each (IP, minute) pair and returns the pairs whose count is greater than or equal to the threshold as (ip, minute, n) tuples, sorted by count from largest to smallest. If you turn the minute into a string with time.strftime("%H:%M"), requests in the same minute fall under the same key. A Counter key can be a tuple (lesson 29: a tuple cannot be changed, which is why it can be a key).
The threshold is a parameter: if the administrator finds 10 too strict, they can run it with 5. The check also tries your function with other thresholds.
This step's code cell is for members.Sign up free