# -*- coding: cp936 -*-
#输入: 日志文件
#处理1: 对日志内容进行匹配,找到匹配内容,存储到字典中
#处理2: 如果匹配内容在字典中,则将键值对应的值加1
#处理3: 在字典中找出值最大的键值
#输出: 键值 即匹配内容
#time 2012-12-17:mon
#引入匹配包
import re
#打开文件
fileop = open(r'F:\\log.txt')
#建立匹配模式
pat = '[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}'
#创建字典
ipcount = {}
#读取文件内容
while True:
item = fileop.readline() #每次从文件中读取一行
if item: #判断是否已达到文件末尾
itemip = re.findall( pat, item)
if ipcount.has_key( itemip[0] ): #查找字典,判断是否已存在相应键值
ipcount[itemip[0]] = ipcount[itemip[0]] +1
else:
ipcount[itemip[0]] = 1 #若没有,则将当前item加入字典ifcount
else:
print '文件读取完毕\n'
#print ipcount