1 import re
2 from collections import Counter
3
4 def count_patt(fname, patt):
5 result = Counter()
6 cpatt = re.compile(patt)
7
8 with open(fname) as fobj:
9 for line in fobj:
10 m = cpatt.search(line)
11 if m:
12 key = m.group()
13 result.update([key])
14
15 return result
16 if __name__ == '__main__':
17 fname = 'access_log'
18 ip = '^(d+.){3}d+'
19 ip_count = count_patt(fname, ip)
20 print(ip_count)
21 print('*' * 30)
22 print(ip_count.most_common(5)) # 前5名
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!