分析apache访问日志

(1)
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+’
print(count_patt(fname, ip))
# awk ‘{print 1}’ access_log | sort | uniq -c | sort -nr  
    br = ‘Firefox|MSIE|Chrome’  
    print(count_patt(fname, br))  
    shell = ‘bash
1}’ access_log | sort | uniq -c | sort -nr      br = ‘Firefox|MSIE|Chrome’      print(count_patt(fname, br))      shell = ‘bash
|nologin$’
print(count_patt(‘/etc/passwd’, shell))

(2)
import re
from collections import Counter

class CountPatt:
def init(self, fname):
self.fname = fname

def count_patt(self, patt):
    cpatt = re.compile(patt)
    patt_obj = Counter()
    with open(self.fname) as fobj:
        for line in fobj:
            m = cpatt.search(line)
            if m:
                item = m.group()
                patt_obj.update([item])
    return patt_obj

if name == ‘main‘:
fname = ‘access_log’
ip = ‘^(\d+.){3}\d+’
web_log = CountPatt(fname)
a = web_log.count_patt(ip)
print(a)
print(a.most_common(3))
b = web_log.count_patt(‘Firefox|MSIE|Chrome’)
print(b)
print(b.most_common(5))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值