- 统计每个客户端访问apache服务器的次数
- 将统计信息通过字典的方式显示出来
- 分别统计客户端是Firefox和MSIE的访问次数
- 分别使用函数式编程和面向对象编程的方式实现
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'
ip_count = count_patt(fname, ip)
ip_list = list(ip_count.items())
ip_list.sort(key=lambda seq: seq[-1], reverse=True)
br_count = count_patt(fname, br)
for i in ip_list:
print(i)
print(br_count)