Complete Python Series · Part 22 of 29
Assignment 09-api-traffic
NextGen CTO — Python Assignments
A collection of practical Python exercises based on real-world scenarios from the NextGen CTO learning platform. Each assignment includes starter code, hints, a complete solution, and an explanation.
Exercise: NextGen CTO API Traffic Analyzer
Learn to analyze server logs using lists, tuples, dictionaries, sets, and frequency counting.
Problem statement
NextGen CTO maintains server logs to monitor API traffic and identify failed requests. Each log contains a client IP address, requested endpoint, and HTTP status code. Build a program that counts each status code and tracks unique IP addresses.
Task requirements
- Process each tuple in the supplied logs using a
forloop and tuple unpacking. - Add each IP address to
unique_ips. - Count each status code in
status_counts, initializing new codes to 1 and incrementing existing counts. - Print the total number of requests, number of unique IP addresses, and status code distribution.
Constraints
- Use a dictionary for status counts and a set for unique IP addresses.
- Count every request, even when its IP address has already appeared.
- Do not use external libraries or
collections.Counter. - Handle an empty log list correctly.
Good luck trying the exercise!
Starter code
logs = [
("192.168.1.10", "/courses", 200),
("192.168.1.15", "/dashboard", 200),
("10.0.0.5", "/login", 404),
("192.168.1.10", "/courses/python", 200),
("10.0.0.5", "/courses/dsa", 200),
("192.168.1.20", "/admin", 403),
("192.168.1.15", "/payments", 500)
]
status_counts = {}
unique_ips = set()
# TODO: Loop through the logs and unpack each tuple
# TODO: Add each IP address to the set
# TODO: Count each status code in the dictionary
# TODO: Print total requests, unique IP count and status distribution
Expected output
Total requests: 7
Unique IP addresses: 4
Status code distribution: {200: 4, 404: 1, 403: 1, 500: 1}
Hints
- Unpack each tuple using
for ip, endpoint, status in logs:. - Use
set.add()to store unique values. - Check whether a status code already exists using
inbefore incrementing its count. - Use
len()to count entries in a list or set.
Solution: NextGen CTO API Traffic Analyzer
Use a set to eliminate repeated IP addresses and a dictionary to accumulate status frequencies in a single pass through the logs.
logs = [
("192.168.1.10", "/courses", 200),
("192.168.1.15", "/dashboard", 200),
("10.0.0.5", "/login", 404),
("192.168.1.10", "/courses/python", 200),
("10.0.0.5", "/courses/dsa", 200),
("192.168.1.20", "/admin", 403),
("192.168.1.15", "/payments", 500)
]
status_counts = {}
unique_ips = set()
# Process each server log
for ip, endpoint, status in logs:
# Store unique IP addresses
unique_ips.add(ip)
# Count occurrences of each status code
if status in status_counts:
status_counts[status] += 1
else:
status_counts[status] = 1
# Display the traffic analysis
print(f"Total requests: {len(logs)}")
print(f"Unique IP addresses: {len(unique_ips)}")
print(f"Status code distribution: {status_counts}")
Solution explanation
- Lines 1–9: Define a list of log tuples containing IP addresses, endpoints, and response codes.
- Lines 11–12: Initialize an empty dictionary and an empty set.
- Line 15: Unpack each log tuple into three variables.
- Line 18: Add the IP address to the set; duplicate values are automatically ignored.
- Lines 21–24: Increment an existing status count or initialize a new count to 1.
- Lines 27–29: Print the total requests, unique IP count, and dictionary of status frequencies.
Test cases
In production, unique IP addresses are not an exact measure of unique users because multiple users can share an IP address.