Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. #!/usr/bin/python2.7
  2.  
  3. def parse_row(s):
  4.     row = []
  5.     qe = qp = None
  6.     for s in s.replace('\r','').replace('\n','').split(' '):
  7.         if qp:
  8.             qp.append(s)
  9.         elif '' == s:
  10.             row.append('')
  11.         elif '"' == s[0]:
  12.             qp = [ s ]
  13.             qe = '"'
  14.         elif '[' == s[0]:
  15.             qp = [ s ]
  16.             qe = ']'
  17.         else:
  18.             row.append(s)
  19.  
  20.         l = len(s)
  21.         if l and qe == s[-1]:
  22.             if l == 1 or s[-2] != '\\':
  23.                 row.append(' '.join(qp)[1:-1].replace('\\'+qe, qe))
  24.                 qp = qe = None
  25.     return row
  26.  
  27. ip = {}
  28. ip_404 = {}
  29. hours = [0] * 24
  30.  
  31. with open('web-access.log') as f:
  32.     for line in f:
  33.         req = parse_row(line)
  34.         addr = req[0]
  35.         status = req[4]
  36.         hour = int(req[2].split(":")[1])
  37.  
  38.         ip[addr] = ip.get(addr, 0) + 1
  39.  
  40.         if status == '404':
  41.             ip_404[addr] = ip_404.get(addr, 0) + 1
  42.  
  43.         hours[hour] = hours[hour] + 1
  44.  
  45. ip = sorted(ip, key=ip.get, reverse=True)
  46. ip_404 = sorted(ip_404, key=ip_404.get, reverse=True)
  47.  
  48. max_req = hours[0]
  49. max_hour = 0
  50. for i in range (0, 24):
  51.     if hours[i] > max_req:
  52.         max_req = hours[i]
  53.         max_hour = i
  54.  
  55. print "1) TOP-5 IP-addresses:"
  56. for i in range(0, 5):
  57.     print ip[i]
  58.  
  59. print "2) Most frequent 404 IP-address:"
  60. print ip_404[0]
  61.  
  62. print "3) The most active hour:"
  63. print max_hour
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement