python小程序:查找nginx日志文件的ip点击率并排序

来源:互联网 发布:小米万能遥控器 编程 编辑:程序博客网 时间:2024/06/06 15:48
def nginxIpCounter(nginxFile):    ipCounter = { }    with open(nginxFile) as f:        for line in f:            ip = line.split(" ",1)[0]            if 6<=len(ip) <= 15:                ipCounter[ip] = ipCounter.get(ip,0) + 1    return ipCounternginxIp = { }nginxIpBySort = { }nginxIp = nginxIpCounter("/root/nginx_ip.test")nginxIpBySort = sorted(nginxIp.iteritems(),key = lambda d:d[1] ,reverse = True)for i,j in nginxIpBySort:    print "%s => %d"%(i,j)
1 0