ITPub博客

首页 > IT职业 > IT职场 > Python编写日志分析小脚本

Python编写日志分析小脚本

IT职场 作者:xzwsun 时间:2021-09-09 09:20:18 0 删除 编辑

import re

def count_patt(fname, patt):
result = {}
cpatt = re.compile(patt)
fobj = open(fname)
for line in fobj:
m = cpatt.search(line)
if m:
key = m.group()
if key not in result:
result[key] = 1
else:
result[key] += 1
fobj.close()
return result

if name == 'main':
fname = '/var/log/httpd/access_log'
ip_patt = '^(d+.){3}d+'
br_patt = 'Firefox|Chrome|MSIE'
print count_patt(fname, ip_patt)
print count_patt(fname, br_patt)

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2567/viewspace-2802360/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-16

  • 博文量
    100
  • 访问量
    923