查一段时间的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