Advertisement
Guest User

lab 2 part 1

a guest
Apr 19th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. #! usr/bin/env python3
  2. # coding=utf-8
  3.  
  4. """
  5. This assignment required us to convert a raw access log
  6. into a dictionary list.
  7.  
  8. To do this, I separated the log into individual lines
  9. and used a regexp pattern to extract the IP address, timestamp,
  10. and GET request from each line.
  11. I then paired each of these elements with a descriptive dictionary keyword.
  12.  
  13. Alex Mote
  14. """
  15.  
  16. import re
  17.  
  18.  
  19. def logtolist(file):
  20.     """
  21.    Separate elements of a given access log file
  22.    and return a dictionary list.
  23.  
  24.    Parameters
  25.    ----------
  26.    file : text file
  27.        The access log file you wish to evaluate.
  28.    
  29.    Returns
  30.    -------
  31.    log_list_dict : list
  32.        A list of dictionary relationships for each element in the log;
  33.        ip_address = The IP address requesting access.
  34.        timestamp = The time at which access was requested.
  35.        request = The command request itself, e.g. "GET /robots.txt HTTP/1.0"
  36.    
  37.    """
  38.     log = open(file, "r")
  39.     log_str = log.read()
  40.     regex = '([\d\.]+) - - \[(.*?)\] "(.*?)" .*'
  41.     # pattern of requests in log
  42.  
  43.     log_list = log_str.split("\n")  # separate log into individual lines
  44.  
  45.     log_ele = []
  46.     for item in log_list:
  47.         log_ele.append(
  48.             re.split(regex, item)
  49.         )  # turn each line into a list of components
  50.     del log_ele[206]  # Last item in list is blank for some reason
  51.  
  52.     key_list = ["ip_address", "timestamp", "request"]
  53.     log_dict_list = []
  54.     for ele in log_ele:  # pair each element with its relevant keyword
  55.         log_dict_list.append(
  56.             {key_list[0]: ele[1], key_list[1]: ele[2], key_list[2]: ele[3]}
  57.         )
  58.  
  59.     return log_dict_list
  60.  
  61.  
  62. if __name__ == "__main__":
  63.     print(logtolist("mini-access-log.txt"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement