python 脚本统计squid日志中的IP访问数和URL访问数量

#!/usr/bin/python  
# -*- coding: utf-8 -*-    
import sys    
from optparse import OptionParser    
try:    
   f = open('/home/squid/access.log')    
except IOError,e:    
   print "can't open the file:%s" %(e)

def log_report(field):  
   if field == "ip":    
       return  [line.split()[2] for line in f]    
   if field == "url":    
       return  [line.split()[6] for line in f]    
def log_count(field):    
   fields2 = {}    
   fields = log_report(field)    
   for field_tmp in fields:    
       if field_tmp in fields2:    
           fields2[field_tmp] += 1    
       else:    
           fields2[field_tmp] = 1    
   return fields2    
def log_sort(field,number = 10 ,reverse = True):    
   for v in sorted(log_count(field).iteritems(),key = lambda x:x[1] , reverse = reverse )[0:int(number)]:    
       print v[1],v[0]    
if __name__ == "__main__":    
   parser =OptionParser(usage="%prog [-i|-u] [-n num | -r]" ,version = "1.0")    
   parser.add_option('-n','--number',dest="number",type=int,default=10,help=" print top line of the ouput")

parser.add_option('-i','--ip',dest="ip",action = "store_true",help="print ip information of access log")  
   parser.add_option('-u','--url',dest="url",action = "store_true",help="print url information of access log")    
   parser.add_option('-r','--reverse',action = "store_true",dest="reverse",help="reverse output ")    
   (options,args) = parser.parse_args()

   if len(sys.argv) < 2:  
       parser.print_help()    
   if options.ip and options.url:    
        parser.error(' -i and -u  can not be execute at the same time ')    
   if options.ip :    
       log_sort("ip", options.number , True and options.reverse  or False)    
   if options.url:    
       log_sort("url", options.number , True and  options.reverse or False)

   f.close()

image

image

image

你可能感兴趣的:(IP,python,url,squid)