Linux培训
达内IT学院
400-996-5531
import re tudou@Gyyx 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 "#/" "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 "#/" "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 "#/" "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 "#/" "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'}
填写下面表单即可预约申请免费试听! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!
Copyright © 京ICP备08000853号-56 京公网安备 11010802029508号 达内时代科技集团有限公司 版权所有
Tedu.cn All Rights Reserved