发布时间:2019-08-01 17:12:52编辑:auto阅读(1596)
日志文件例子:
#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 ipaddress f.close() #分析ipaddress 里的IP,构建一个key-value字典 iptab = {} for ipstr in ipaddress: if ipstr in iptab: iptab[ipstr] += 1 else: iptab[ipstr] = 1 print 'iptab = %s' % iptab #对values的所有值进行倒序排列 ipnum = iptab.values() ipnum.sort(reverse=True) print ipnum #找出values值前三的key,即访问量前三的IP toplist=[] 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
上一篇: python学习笔记-3运算符与表达式
下一篇:
读书笔记
47491
45793
36791
34322
28959
25596
24442
19609
19110
17631
5464°
6047°
5569°
5637°
6572°
5375°
5377°
5883°
5854°
7169°