title: How do you print the top most used clients IP with a certain status code.?
body: <pre><code>import os
  import re 
  from collections import Counter 
  from collections import OrderedDict 
  fileNames = []
  textInfo = []
  d = {}
  
  currentDirectoryPath = os.getcwd()
  print(currentDirectoryPath)
  
  
  regexp = re.compile(
      r'(?P&lt;clientIP&gt;\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).+\['
      + '(?P&lt;timestamp&gt;\d{2}/[A-Z][a-z]{2}/\d\d\d\d).+\"'
      + '(?P&lt;action&gt;[A-Z]{3,4}).+\"'  
      + '\s*(?P&lt;statuscode&gt;[1-5][0-9][0-9])'
      )
  
  
  
  os.chdir("/content/drive/log")
  currentDirectoryPath = os.getcwd()
  listOfFileNames = os.listdir(currentDirectoryPath)
  #for files in listOfFileNames :
      #print(files) 
  
  
  f = open('access_1.log', 'r')
  matched = 0
  failed = 0
  cnt_clientIPs = Counter()
  cnt_clientAction = Counter()
  cnt_clientTimeStamp = Counter()
  cnt_clientStatusCode = Counter()
  
  
  for line in f:
      m = re.match(regexp, line)
      if m:
          cnt_clientIPs.update([m.group('clientIP')])
          cnt_clientAction.update([m.group('action')])
          cnt_clientStatusCode.update([m.group('statuscode')])
          matched += 1
      else:
          failed += 1
          continue
  
      print("""""\
  client .........: %s
  timestamp ......: %s
  action .........: %s
  statuscode.........: %s
  """ % ( m.group('clientIP'),
          m.group('timestamp'),
          m.group('action'),
          m.group('statuscode'),
      ))
  
  for line in f:
      m = re.match(regexp, line)
      if m:
        d = {m.group("clientIP"): m.group("statuscode")}
  
  print(d)
  
  
  userInputIP = input("Enter how many of the top clients you want to see. ")
  print('[*] %d lines matched the regular expression' % (matched))
  print('[*] %d lines failed to match the regular expression' % (failed), end='\n\n')
  print('[*] ============================================')
  print('[*] '+ userInputIP +' Most Frequently Occurring Clients Queried')
  print('[*] ============================================')
  
  for clientIP, count in cnt_clientIPs.most_common(int(userInputIP)):
      print('[*] %30s: %d' % (clientIP, count))
  print('[*] ============================================')
  
  
  userInputAction = input("Enter how many of the top actions you want to see. ")
  print('[*] '+ userInputAction +' Most Frequently Occurring Clients Actions')
  print('[*] ============================================')
  
  for action, count in cnt_clientAction.most_common(int(userInputAction)):
      print('[*] %30s: %d' % (action, count))
  print('[*] ============================================')
  
  
  userInputIpPlusStatus = input("Enter how many of the top clients  you want to see. and there status code ")
  print('[*] '+ userInputIpPlusStatus +' Most Frequently Occurring Clients IP and Status Code')
  print('[*] ============================================')
  
  
  for clientIP,  count in cnt_clientIPs.most_common(int(userInputIpPlusStatus)):
      if (stuatuscode == userStatuscodeInput):
         print('[*] %30s: %d: %5s:' % (clientIP, count,  m.group('statuscode')))
  print('[*] ============================================')
  
  Enter how many of the top clients you want to see.5
  [*] 49997 lines matched the regular expression
  [*] 3 lines failed to match the regular expression
  
  [*] ============================================
  [*] 5 Most Frequently Occurring Clients Queried
  [*] ============================================
  [*]                 205.167.170.15: 15695
  [*]                  79.142.95.122: 3207
  [*]                  52.22.118.215: 734
  [*]                  84.112.161.41: 712
  [*]                   37.1.206.196: 371
  [*] ============================================
  Enter how many of the top actions you want to see.5
  [*] 5 Most Frequently Occurring Clients Actions
  [*] ============================================
  [*]                            GET: 44048
  [*]                           POST: 5921
  [*]                           HEAD: 25
  [*]                            PUT: 3
  [*] ============================================
  
  {}
  </code></pre>
  
  <p>I want to print out the Top 5 client IPs with status code 404. Or any status code is given from the user. Here Is some test line below to help. Can that also be done within a certain timeframe? For example to print the Top clients IP with a status code of 404 from January to February?  </p>
  
  <pre><code>for clientIP,  count in cnt_clientIPs.most_common(int(userInputIpPlusStatus)):
      if (stuatuscode == userStatuscodeInput):
         print('[*] %30s: %d: %5s:' % (clientIP, count,  m.group('statuscode')))
  print('[*] ============================================')
  </code></pre>
  
  <p>This part right here is where I am talking about. Trying to add the condition here. </p>
  
  <pre><code>80.110.186.51 - - [21/Dec/2015:17:20:12 +0100] "GET /images/stories/raith/oststeiermark.png HTTP/1.1" 200 65225 "http://www.almhuette-raith.at/" "Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1" "-"
  80.110.186.51 - - [21/Dec/2015:17:20:12 +0100] "GET /images/stories/raith/garage.jpg HTTP/1.1" 200 57339 "http://www.almhuette-raith.at/" "Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1" "-"
  80.110.186.51 - - [21/Dec/2015:17:20:12 +0100] "GET /images/stories/slideshow/almhuette_raith_03.jpg HTTP/1.1" 200 87782 "http://www.almhuette-raith.at/" "Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1" "-"
  80.110.186.51 - - [21/Dec/2015:17:20:12 +0100] "GET /images/stories/raith/steiermark_herz.png HTTP/1.1" 200 39683 "http://www.almhuette-raith.at/" "Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1" "-"  
  </code></pre>
  
  <p>These lines above are some test lines to help you guys out and to show what I am dealing with in the text files. </p>