【python学习】日志文件里IP访问最
短信预约 -IT技能 免费直播动态提醒
日志文件例子:
#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
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341