前言:python统计apache、nginx访问日志IP访问次数并且排序(显示前20条)。其实用awk+sort等命令可以实现,用awk数组也可以实现,这里只是用python尝试下。

apache脚本:

ips = {}with open("/root/mail_access_log-20180629") as fh:    for line in fh:        ip = line.split(" ")[0]        if 6 < len(ip) <=15:            ips[ip] = ips.get(ip, 0) + 1ip_num = []for ipaddr,num in ips.items():   ip_num.append((ipaddr,num))ip_num.sort(key=lambda x: x[1], reverse=True)for ipaddr,num in ip_num[:20]:    print('IP地址为{}, 访问次数为{}'.format(ipaddr,num))

nginx脚本:

ips = {}with open("/root/access.log-20180629") as fh:    for line in fh:        ip = line.split(" ")[0]        if 6 < len(ip) <=15:            ips[ip] = ips.get(ip, 0) + 1ip_num = []for ipaddr,num in ips.items():   ip_num.append((ipaddr,num))ip_num.sort(key=lambda x: x[1], reverse=True)for ipaddr,num in ip_num[:20]:    print('IP地址为{}, 访问次数为{}'.format(ipaddr,num))