python 脚本分析dns日志计算前几位的请求数域名

查一段时间的dns上的域名访问次数排行(top100)

1. 原始文件数据分析

 1) 这个功能是否是长期需要使用的,如果是需要长期定时使用,可以优化dns或者监控相关的代码,如将计算相关的日志单独输出。

2)找规律,编写正则算法。

3) 读取日志文件,日志文件是否压缩。

4)如何保存结果,字典,保存在内存,还是持久化到数据库。

 

#write by siashero
import gzip
import re

# file = open('/home/xxx/')
file = gzip.open("e:\python_programs\queries.log.CBN-XA-1-3N3.20130803160052.gz")





domain_list= {}
print "time format is 13-08-04 19:1{1,2,3,4,5} "
time = raw_input("please enter a time you want to analysis")


while True:
    line = file.readline()
    if not line:
    break
    if re.search(time,line):
        domain = line.split(‘|')[2]
            if domain in domain_list:
                domain_list[domain] += 1
            else:
                domain_list[domain] = 1
                count = 0

for v in sorted(domain_list.iteritems(),key =lambda x:x[1],reverse=True):

print v[1],v[0]

#to print the only top20 domain
if count > 20:
    break
count += 1
raw_input("enter a word to finish")
file.close

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值