In these two weeks, several of them wanted to learn python, so we created an environment and atmosphere to learn from them.
Yesterday in the group, posted a demand, is the statistics Squid access log IP access number and URL of the number of visits and sorting, many students are generally realized the corresponding functions, I put my simple implementation of the post, welcome to Pat Bricks:
The log format is as follows:
Copy Code code as follows:
%ts.%0 3tu%6tr%{x-forwarded-for}>h%ss/%03hs%<st%rm%ru%un%sh/%<a%mt "%{referer}>h" "%{User-Agent}>h"%{Co Okie}>h
Copy Code code as follows:
1372776321.285 0 100.64.19.225 tcp_hit/200 8560 get http://img1.jb51.net/games/0908/19/1549401_3_80x100.jpg-none/- Image/jpeg "http://www.jb51.net/" "mozilla/4.0" (compatible; MSIE 8.0; Windows NT 5.1; trident/4.0; Qqdownload 734; net4.0c. NET CLR 2.0.50727) "pcsuv=0;%20pcuvdata=lastaccesstime=1372776317582;%20u4ad=33480hn;%20c=14arynt;%20uf= 1372776310453
Copy Code code as follows:
#!/usr/bin/python
#-*-Coding:utf-8-*-
Import Sys
From Optparse import Optionparser
'''
Just a test on the log file, the number of IP Access.log in the statistics Department
'''
Try
f = open ('/data/proclog/log/squid/access.log ')
Except Ioerror,e:
Print "Can ' t open the file:%s"% (e)
def log_report (field):
'''
Return the field of the access log
'''
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):
'''
Return a dict of like {field:number}
'''
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 = ten, reverse = True):
'''
Print the sorted fields to output
'''
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 being 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 ()
The effect is as follows: