python脚本实现统计日志文件中的ip访问次数代码分享
适用的日志格式:
106.45.185.214--[06/Aug/2014:07:38:59+0800]"GET/HTTP/1.0"20010"-""-" 171.104.119.22--[06/Aug/2014:08:55:01+0800]"GET/HTTP/1.0"20010"-""-" 27.31.238.242--[06/Aug/2014:09:43:19+0800]"GET/HTTP/1.0"20010"-""-" 218.65.202.131--[06/Aug/2014:10:33:59+0800]"GET/HTTP/1.0"20010"-""-"
以上为nginx的日志,本程序只适合用于IP在开头并用空格分开的这种格式。
实例代码:
importsys
classLog:
def__init__(self,filename,dic,count):
self.filename=filename
self.dic=dic
self.count=count
defparse(self):
i=1
f=file(self.filename)
whileTrue:
line=f.readline()
iflen(line)==0:
break
ip=line.split('')
ifip[0]indic:
self.dic[ip[0]]=self.dic[ip[0]]+1
else:
self.dic[ip[0]]=i
soredic=sorted(self.dic.items(),key=lambdad:d[1],reverse=True)
counts=0;
foriteminsoredic:
ifcounts==int(self.count):
break
print("IP:%sTotalTimes:%s"%(item[0],item[1]))
counts=counts+1
f.close()
if__name__=="__main__":
iflen(sys.argv)<3:
print('usage:log.pylog.logtoptimes\nexamplelog.pylog.log20\ncodebyiswin')
sys.exit()
dic={}
log=Log(sys.argv[1],dic,sys.argv[2])
log.parse()
热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬