Advertisement
vlsmooth

mduck_week1

Aug 12th, 2022
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. import re
  2. def logs():
  3.     pattern = r'(?P<host>[\d.]+) - (?P<user_name>\w+|-) \[(?P<time>[^]]+)\] "(?P<request>[^"]+)'
  4.     with open("assets/logdata.txt", "r") as file:
  5.         logdata = file.read()
  6.         matches = re.finditer(pattern, logdata)
  7.         return([m.groupdict() for m in matches])
  8.  
  9. def logs_mduck():
  10.     with open("assets/logdata.txt", "r") as file:
  11.         logdata = file.read()
  12.     host_list=re.findall('(\d+\.\d+\.\d+\.\d+)', logdata)
  13.     user_list=re.findall('((?<=-\s)[\w-]+)', logdata)
  14.     time_list=re.findall('((?<=\[)[\d\w\/\:\s\-]+)', logdata)
  15.     request_list=re.findall('"(.*?)"', logdata)
  16.     logdata_dict=[]
  17.     i=0
  18.     while i< len(host_list):
  19.         logdata_dict.append({'host': host_list[i],'user_name': user_list[i],'time': time_list[i],'request': request_list[i]})
  20.         i += 1
  21.     for i in (25,26,27,28):
  22.         print(f"{i:2}. '{user_list[i]}'")
  23.     return logdata_dict
  24.  
  25. expectation = logs()
  26. debug = logs_mduck()
  27. for i, curr_expect in enumerate(expectation):
  28.     curr_debug = debug[i]
  29.     if curr_expect != curr_debug:
  30.         raise Exception(f"mismatch: i={i}:\ncurr_expect: {curr_expect}\n curr_debug: {curr_debug}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement