python统计日志ip访问数的方法

本文实例讲述了python统计日志ip访问数的方法。分享给大家供大家参考。具体如下:

import re
f=open(“/tmp/a.log”,”r”)
arr={}
lines = f.readlines()
for line in lines:
ipaddress=re.compile(r’^#(((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?))’)
match=ipaddress.match(line)
if match:
ip = match.group(1)
if(arr.has_key(ip)):
arr[ip]+=1
else:
arr.setdefault(ip,1)
f.close()
for key in arr:
print key+”->”+str(arr[key])

日志格式为:

#111.172.249.84 – – [12/dec/2011:05:33:36 +0800] “get /images/i/gotop.png http/1.0” 200 486 “http://wh.xxxx.com/” “mozilla/4.0 (compatible; msie 7.0; windows nt 6.1; trident/5.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729; media center pc 6.0; .net4.0c; .net4.0e)”
#111.172.249.84 – – [12/dec/2011:05:33:36 +0800] “get /images/i/gotop.png http/1.0” 200 486 “http://wh.xxxx.com/” “mozilla/4.0 (compatible; msie 7.0; windows nt 6.1; trident/5.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729; media center pc 6.0; .net4.0c; .net4.0e)”
#111.172.249.85 – – [12/dec/2011:05:33:36 +0800] “get /images/i/gotop.png http/1.0” 200 486 “http://wh.xxxx.com/” “mozilla/4.0 (compatible; msie 7.0; windows nt 6.1; trident/5.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729; media center pc 6.0; .net4.0c; .net4.0e)”
#111.172.249.86 – – [12/dec/2011:05:33:36 +0800] “get /images/i/gotop.png http/1.0” 200 486 “http://wh.xxxx.com/” “mozilla/4.0 (compatible; msie 7.0; windows nt 6.1; trident/5.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729; media center pc 6.0; .net4.0c; .net4.0e)”

输出结果:

111.172.249.86->1
111.172.249.84->2
111.172.249.85->1

注释:python字段的setdefault用法为获取信息,如果获取不到的时候就按照他的参数设置该值

>>> a={}
>>> a[‘key’]=’123′
>>> print (a)
{‘key’: ‘123’}
>>> print (a.setdefault(‘key’,’456′))
#显示a这个字典的’key’值的内容,因为字典有,所以不会去设置它
123
>>> print (a.setdefault(‘key1′,’456’))
#显示a这个字典的’key1’值的内容,因为字典没有,所以设置为456了
456
>>> a
{‘key1’: ‘456’, ‘key’: ‘123’}

希望本文所述对大家的python程序设计有所帮助。