Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use your favorite programming/scripting language to write a program to an

ID: 3886791 • Letter: P

Question

Please use your favorite programming/scripting language to write a program to analyze web server log, and print out the remote client IP, and the number of successful requests (status code 200) made by each remote client.

The link below defines the log format for this exercise:

   http://en.wikipedia.org/wiki/Common_Log_Format

For example, if the log file contains the following 5 log entries:

192.168.1.2 - - [17/Sep/2013:22:18:19 -0700] "GET /abc HTTP/1.1" 404 201

192.168.1.2 - - [17/Sep/2013:22:18:19 -0700] "GET /favicon.ico HTTP/1.1" 200 1406

192.168.1.2 - - [17/Sep/2013:22:18:27 -0700] "GET /wp/ HTTP/1.1" 200 5325

192.168.1.2 - - [17/Sep/2013:22:18:27 -0700] "GET /wp/wp-content/themes/twentytwelve/style.css?ver=3.5.1 HTTP/1.1" 200 35292

192.168.1.3 - - [17/Sep/2013:22:18:27 -0700] "GET /wp/wp-content/themes/twentytwelve/js/navigation.js?ver=1.0 HTTP/1.1" 200 863

Your program’s output should print:

192.168.1.2       3

192.168.1.3       1

Explanation / Answer

# To run this python script, enter following command in terminal.
# python script.py ./logfilename
import sys
if(len(sys.argv)<2):
   print("Please provide a log file path.")
   print("For example: python script.py ./logfilename")
   exit()
dictionary = {}
input_file = open(sys.argv[1],"r")
for line in input_file:
   if line.strip():
       lst = line.strip().split()
       if(int(lst[-2])==200):
           if lst[0] in dictionary.keys():
               val = dictionary[lst[0]]
               dictionary[lst[0]] = int(val)+1
           else:
               dictionary[lst[0]] = 1
for key in dictionary:
   print key,
   print dictionary[key]