Exercise: Parse the logs
Web servers write one line per request, like this:
1.2.3.4 - - [01/Mar/2024:10:00:01] "GET /home HTTP/1.1" 200 512
The quoted part holds the method (GET) and the path (/home). The number
after it is the status code (200). Answering questions about logs is a
common practical interview task.
Exercise: finish three functions.
parse_line(line)returns(method, path, status)withstatusas anint, orNoneif the line doesn't look like a request.status_counts(lines)returns a dict of how many lines have each status code. Skip lines that don't parse.busiest_path(lines)returns the path requested most often. On a tie, return the one that sorts first. ReturnNoneif no line parses.
Use re.search with groups for the parsing, and Counter for the counting.
A pattern like r'"(\w+) (\S+) [^"]*" (\d{3})' is a good start.
Press Check to run the tests.