Show and Prove
import re
from collections import defaultdict
def extract_info(line):
pattern = r'(\d+\.\d+\.\d+\.\d+) .*? \[(.*?)\] "[A-Z]+ ([^ ]+) .*" (\d+) (\d+)'
match = re.match(pattern, line)
if match:
ip = match.group(1)
timestamp = match.group(2)
file_size = int(match.group(5))
status_code = int(match.group(4))
return ip, timestamp, file_size, status_code
else:
return None
log_file = 'access.log'
ip_requests = defaultdict(list)
with open(log_file, 'r') as file:
for line in file:
info = extract_info(line)
if info:
ip, timestamp, file_size, status_code = info
if status_code == 200:
ip_requests[ip].append((timestamp, file_size))
most_requests_ip = max(ip_requests, key=lambda x: len(ip_requests[x]))
largest_file_access_time = None
largest_file_size = 0
for timestamp, file_size in ip_requests[most_requests_ip]:
if file_size > largest_file_size:
largest_file_size = file_size
largest_file_access_time = timestamp
print("Access time for the largest file downloaded by the IP with the most successful requests:")
print(largest_file_access_time)
Last updated