Python 官方文档:入门教程 => 点击学习
日志文件例子:#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
日志文件例子:
#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
--结束END--
本文标题: 【python学习】日志文件里IP访问最
本文链接: https://lsjlt.com/news/184585.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0