python statistics apache, nginx access log IP visits and sorting (20 before the show) [turn]

Preface: python statistics apache, nginx access log IP visits and sorting (20 before the show). In fact, it can be achieved using awk + sort commands can also be implemented in awk array here just to try using the following python.

 

apache script:

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) + 1

ip_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( ' The IP address of {}, visits and {} ' .format (ipaddr, NUM))

 

nginx script:

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) + 1

ip_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( ' The IP address of {}, visits and {} ' .format (ipaddr, NUM))

 

Transfer from

python statistics apache, nginx access log IP visits and sorting (front display 20) - Seri -51CTO blog
https://blog.51cto.com/net881004/2134826

Guess you like

Origin www.cnblogs.com/paul8339/p/10955848.html
Recommended