def count_status_codes(log_file): status_counts = defaultdict(int) with open(log_file, 'r') as f: for line in f: # Extract the HTTP response status code from each line status_code = line.split()[-2] status_counts[status_code] += 1 return status_counts
if __name__ == '__main__': if len(sys.argv) != 2: print('Usage: python script.py log_file') sys.exit(1) log_file = sys.argv[1] status_counts = count_status_codes(log_file) for status_code, count in status_counts.items(): print(f'{status_code}: {count}')
import sys import json from collections import defaultdict
def count_status_codes(log_file): status_counts = defaultdict(int) with open(log_file, 'r') as f: for line in f: # Extract the HTTP response status code from each line status_code = line.split()[-2] status_counts[status_code] += 1 return status_counts
import time ## All other imports and function definitions here start_time = time.time() main() print("--- %s seconds ---" % (time.time() - start_time))