Python分析NGINX日志里面相同IP第一次访问时间和最后一次访问时间
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python分析NGINX日志里面相同IP第一次访问时间和最后一次访问时间相关的知识,希望对你有一定的参考价值。
有个小伙伴在群里问如何分析一下相同IP访问NGINX的第一次访问时间和最后一次访问时间,折腾了好一会最后写出来测试一下,OK没问题的,所以就记录在这里。
思路:
1、文件读取
2、先找出只访问一次的 只有一次时间
3、访问多次的 寻找开始时间和 最后一次时间
1、将所有IP和时间分别定义一个列表 两者元素相同
2、遍历搜索IP的index位置,同时去找时间位置
3、最后一次时间需要对IP和时间列表翻转
列表翻转方法:
1、 使用reversed()函数:reversed()函数返回的是一个迭代器,而不是一个List,需要再使用List函数转换一下
2、使用sorted() 其中reverse=True是按降序排列,reverse=False是按照升序排列
3、使用分片: 其中[::-1]代表从后向前取值,每次步进值为1
#/usr/bin/env python #-*-coding:UTF-8 -*- ips=[] datelist=[] ips_once=[] #仅出现一次的IP集合 ipindex_list=[] #IP信息列表 commdatetime=[] #时间信息列表 和IP信息是一一对应 infodict={} #创建了一个序列号 IP+时间的字典 ip_count_first=[] #仅出现一次的IP+时间集合 ip_count_many = [] #出现多次 IP第一次时间 第二次时间的集合 print("欢迎使用NGINX日志分析小工具!!!!") print("功能一:统计每个IP访问的次数") """ with open("newlog.txt","r") as ngfile: for line in ngfile: iptime=line.split(' ')[0] ip =iptime.split('-')[0] time = iptime.split('[')[1] print(ip,time)""" with open("nginxlog.txt","r") as ngfile: for line in ngfile: ips.append(line.split()[0]) datelist.append(line.split()[1]) for i in range(0,int(len(ips))): infodict[i]= "IP:"+ips[i]+" 访问时间:"+datelist[i] for ip in set(ips): ip_count = str(ips).count(ip) print("IP地址: "+ip+" 访问书次数:"+ str(ip_count)) if ip_count == 1: ip_index = ips.index(ip) ip_count_first.append(infodict[ip_index]) ips_once.append(ip) else: first_index = ips.index(ip) first_time = datelist[first_index] ips_reverse= ips[::-1] last_index = ips_reverse.index(ip) datelist_reverse = datelist[::-1] last_time = datelist_reverse[last_index] ip_count_many.append('ip:'+ip+" 第一次访问时间: "+first_time+' 最后一次访问时间:'+last_time) #print(ip_count_first) #多次记录 print('功能二:统计只访问1次的IP地址和时间:'.format('*',3)) for ip_time in ip_count_first: print(ip_time) print('功能三:访问多次的IP地址,第一次时间和最后一次时间:') for j in ip_count_many: print(j)
日志IP和时间部分
103.106.120.4 24/May/2018:06:12:10 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:12 103.106.120.4 24/May/2018:06:12:13 103.106.120.4 24/May/2018:06:12:14 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:12 103.106.120.4 24/May/2018:06:12:12 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:19:16 123.168.70.118 24/May/2018:06:16:57 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:17:04 123.168.70.118 24/May/2018:06:17:05 123.168.70.118 24/May/2018:06:17:05 123.168.70.118 24/May/2018:06:17:05 123.168.70.118 24/May/2018:06:17:05 123.168.70.118 24/May/2018:06:17:05 123.168.70.118 24/May/2018:06:17:43 123.168.70.118 24/May/2018:06:17:43 112.36.36.74 24/May/2018:06:18:00 183.230.30.249 24/May/2018:06:21:56 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:11 103.106.120.4 24/May/2018:06:12:12 103.106.120.4 24/May/2018:06:12:12 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:15 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:16 153.119.226.180 24/May/2018:06:13:19 123.168.70.118 24/May/2018:06:16:57 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:58 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.113 24/May/2018:06:16:59 123.168.70.118 24/May/2018:06:16:59 123.168.70.110 24/May/2018:06:17:04 123.168.70.117 24/May/2018:06:17:05 123.168.71.118 24/May/2018:06:17:05 123.168.70.118 24/May/2018:06:17:05 123.168.70.118 24/May/2018:06:17:05 123.168.70.118 24/May/2018:06:17:05 123.168.70.118 24/May/2018:06:17:43 123.168.70.118 24/May/2018:06:17:43 112.36.36.74 24/May/2018:06:18:00 183.230.30.249 24/May/2018:06:21:56 112.36.36.70 24/May/2018:06:18:00 112.36.36.70 24/May/2018:06:18:02 110.36.38.70 24/May/2018:06:18:00
结果:
欢迎使用NGINX日志分析小工具!!!! 功能一:统计每个IP访问的次数 IP地址: 123.168.71.118 访问书次数:1 IP地址: 110.36.38.70 访问书次数:1 IP地址: 153.119.226.180 访问书次数:68 IP地址: 112.36.36.70 访问书次数:2 IP地址: 123.168.70.117 访问书次数:1 IP地址: 123.168.70.113 访问书次数:1 IP地址: 123.168.70.118 访问书次数:82 IP地址: 123.168.70.110 访问书次数:1 IP地址: 112.36.36.74 访问书次数:2 IP地址: 103.106.120.4 访问书次数:42 IP地址: 183.230.30.249 访问书次数:2 功能二:统计只访问1次的IP地址和时间: IP:123.168.71.118 访问时间:24/May/2018:06:17:05 IP:110.36.38.70 访问时间:24/May/2018:06:18:00 IP:123.168.70.117 访问时间:24/May/2018:06:17:05 IP:123.168.70.113 访问时间:24/May/2018:06:16:59 IP:123.168.70.110 访问时间:24/May/2018:06:17:04 功能三:访问多次的IP地址,第一次时间和最后一次时间: ip:153.119.226.180 第一次访问时间: 24/May/2018:06:13:15 最后一次访问时间:24/May/2018:06:13:19 ip:112.36.36.70 第一次访问时间: 24/May/2018:06:18:00 最后一次访问时间:24/May/2018:06:18:02 ip:123.168.70.118 第一次访问时间: 24/May/2018:06:16:57 最后一次访问时间:24/May/2018:06:17:43 ip:112.36.36.74 第一次访问时间: 24/May/2018:06:18:00 最后一次访问时间:24/May/2018:06:18:00 ip:103.106.120.4 第一次访问时间: 24/May/2018:06:12:10 最后一次访问时间:24/May/2018:06:12:12 ip:183.230.30.249 第一次访问时间: 24/May/2018:06:21:56 最后一次访问时间:24/May/2018:06:21:56
以上是关于Python分析NGINX日志里面相同IP第一次访问时间和最后一次访问时间的主要内容,如果未能解决你的问题,请参考以下文章