日志文件例子:
#111.172.249.84 - - [12/Dec/2011:05:33:36 +0800] "GET /p_w_picpaths/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 /p_w_picpaths/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 /p_w_picpaths/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 /p_w_picpaths/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 111.172.249.86 /p_w_picpaths/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)"
代码:
import re#列出所有IP,放入ipaddress f=open("/tmp/a.log","r")ipaddress = []lines = f.readlines()for line in lines: ipaddress.extend(re.findall(r'([1-2]?\d?\d\.[1-2]?\d?\d\.[1-2]?\d?\d\.[1-2]?\d?\d)',line))print ipaddressf.close()#分析ipaddress 里的IP,构建一个key-value字典iptab = {}for ipstr in ipaddress: if ipstr in iptab: iptab[ipstr] += 1 else: iptab[ipstr] = 1print 'iptab = %s' % iptab#对values的所有值进行倒序排列ipnum = iptab.values()ipnum.sort(reverse=True)print ipnum#找出values值前三的key,即访问量前三的IPtoplist=[]for num in ipnum[0:3]: for key in iptab: if iptab[key] == num and key not in toplist : print "%s has %s times" % (key,num) toplist.append(key) break