python脚本分析apache访问日志,统计访问量
本脚本读取apache访问日志,分析统计ip访问次数,和浏览器访问次数,并降序输出
#!/root/bin/python
import re
def count_patt(fname,patt):
patt_dict = {}
cpatt = re.compile(patt)
with open(fname) as fobj:
for line in fobj:
m = cpatt.search(line)
if m:
key = m.group()
patt_dict[key] = patt_dict.get(key,0) + 1
return patt_dict
if __name__ == '__main__':
fname = 'access_log'
ip = '^(\d+\.){3}\d+'
br = 'Firefox|MSIE|Chrome'
result1 = count_patt(fname,ip)
result2 = count_patt(fname,br)
iplist = list(result1.items())
iplist.sort(key=lambda seq:seq[-1],reverse=True)
bowser = list(result2.items())
bowser.sort(key=lambda seq:seq[-1],reverse=True)
print(iplist)
print(bowser)