Python parses the same IP first access time and last access time inside the Nginx log

Source: Internet
Author: User

A little buddy in the group asked how to analyze the same IP access Nginx first access time and last access time, toss a good one will finally write to test, OK no problem, so it is recorded here.

Ideas:

1. File read

2, first to find only one visit only once time

3, visit multiple search start time and the last time

1. Define a list of all IP and time separately the same elements

2. Traverse the index location of the search IP and find the time position

3. The last time you need to flip the IP and time list

List Rollover method:

1. Use the reversed () function: the reversed () function returns an iterator instead of a list, which needs to be converted using the list function.

2. Use sorted () where reverse=true is sorted in descending order, Reverse=false is in ascending order

3. Use shards: Where [::-1] represents the value from the back forward, each step value is 1

#/usr/bin/env python#-*-coding:utf-8 -*-ips=[]datelist=[]ips_once=[]  #仅出现一次的IP集合ipindex_list =[]   #IP信息列表commdatetime =[]  #时间信息列表   and IP information is one by one corresponding to infodict={}     #创建了一个序列号    ip+ Time Dictionary ip_count_first=[]  #仅出现一次的IP + Time collection ip_count_many = []  #出现多次  ip first time     Second time collection print ("Welcome to NGINX log analysis Gadget!!!!") ") Print (" function one: Count the number of times per IP access ")" "" 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]+ "  Access time:" + Datelist[i]   for ip in set (IPs):       ip_count  = STR (IPs). Count (IP)       print ("IP address: " +ip+ "  Access Book Number:" +  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 access time:  ' +first_time+ '   Last access time: ' +last_time ')            #print (Ip_count_first) #多次记录print (' function two: statistics are only accessed 1 times by IP address and time: '. Format (' * ', 3)) for   ip_time in ip_count_first:   print (ip_time)    print (' Function Three: Access multiple IP addresses, first time and last time: ') For  j in  ip_count_many:   print (j)

Log IP and time parts

103.106.120.4 24/may/2018:06:12:10103.106.120.4 24/may/2018:06:12:11103.106.120.4 24/may/ 2018:06:12:12103.106.120.4 24/may/2018:06:12:13103.106.120.4 24/may/2018:06:12:14103.106.120.4  24/may/2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4 24/may/ 2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4  24/may/2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4 24/may/ 2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4  24/may/2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4 24/may/ 2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4 24/may/2018:06:12:12103.106.120.4  24/may/2018:06:12:12153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/ 2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180  24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/ 2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180  24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/ 2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180  24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/ 2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180  24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/ 2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:16153.119.226.180  24/may/2018:06:13:16153.119.226.180 24/may/2018:06:13:16153.119.226.180 24/may/ 2018:06:13:16153.119.226.180 24/MAY/2018:06:13:16153.119.226.180 24/MAY/2018:06:13:16153.119.226.180 24/may/2018:06:13:16153.119.226.180 24/may/2018:06:13:16153.119.226.180  24/may/2018:06:13:16153.119.226.180 24/may/2018:06:13:16153.119.226.180 24/may/ 2018:06:19:16123.168.70.118 24/may/2018:06:16:57123.168.70.118 24/may/2018:06:16:58123.168.70.118  24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/ 2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118  24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/ 2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118  24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/ 2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118  24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118  24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:59123.168.70.118 24/may/ 2018:06:16:59123.168.70.118 24/may/2018:06:16:59123.168.70.118 24/may/2018:06:16:59123.168.70.118  24/may/2018:06:16:59123.168.70.118 24/may/2018:06:16:59123.168.70.118 24/may/ 2018:06:16:59123.168.70.118 24/may/2018:06:16:59123.168.70.118 24/may/2018:06:16:59123.168.70.118  24/may/2018:06:16:59123.168.70.118 24/may/2018:06:16:59123.168.70.118 24/may/ 2018:06:16:59123.168.70.118 24/may/2018:06:17:04123.168.70.118 24/may/2018:06:17:05123.168.70.118  24/may/2018:06:17:05123.168.70.118 24/may/2018:06:17:05123.168.70.118 24/may/ 2018:06:17:05123.168.70.118 24/may/2018:06:17:05123.168.70.118 24/may/2018:06:17:43123.168.70.118  24/may/2018:06:17:43112.36.36.74 24/may/2018:06:18:00183.230.30.249 24/may/2018:06:21:56103.106.120.4 24/may/2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4 24 /may/2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4  24/may/2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4 24/may/ 2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4  24/may/2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4 24/may/ 2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4  24/may/2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4 24/may/ 2018:06:12:11103.106.120.4 24/may/2018:06:12:11103.106.120.4 24/may/2018:06:12:12103.106.120.4  24/may/2018:06:12:12153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/ 2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/ 2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180  24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/ 2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180  24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/ 2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180  24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/ 2018:06:13:15153.119.226.180 24/may/2018:06:13:15153.119.226.180 24/may/2018:06:13:16153.119.226.180  24/may/2018:06:13:16153.119.226.180 24/may/2018:06:13:16153.119.226.180 24/may/ 2018:06:13:16153.119.226.180 24/may/2018:06:13:16153.119.226.180 24/may/2018:06:13:16153.119.226.180 24/may/2018:06:13:16153.119.226.180 24/may/ 2018:06:13:16153.119.226.180 24/may/2018:06:13:16153.119.226.180 24/may/2018:06:13:16153.119.226.180  24/may/2018:06:13:19123.168.70.118 24/may/2018:06:16:57123.168.70.118 24/may/ 2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118  24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/ 2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118  24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/ 2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118  24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/ 2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/ 2018:06:16:58123.168.70.118 24/may/2018:06:16:58123.168.70.118 24/may/2018:06:16:59123.168.70.118  24/may/2018:06:16:59123.168.70.118 24/may/2018:06:16:59123.168.70.118 24/may/ 2018:06:16:59123.168.70.118 24/may/2018:06:16:59123.168.70.118 24/may/2018:06:16:59123.168.70.118  24/may/2018:06:16:59123.168.70.118 24/may/2018:06:16:59123.168.70.118 24/may/ 2018:06:16:59123.168.70.118 24/may/2018:06:16:59123.168.70.113 24/may/2018:06:16:59123.168.70.118  24/may/2018:06:16:59123.168.70.110 24/may/2018:06:17:04123.168.70.117 24/may/ 2018:06:17:05123.168.71.118 24/may/2018:06:17:05123.168.70.118 24/may/2018:06:17:05123.168.70.118  24/may/2018:06:17:05123.168.70.118 24/may/2018:06:17:05123.168.70.118 24/may/ 2018:06:17:43123.168.70.118 24/may/2018:06:17:43112.36.36.74 24/may/2018:06:18:00183.230.30.249 24/may/2018:06:21:56112.36.36.70 24/may/2018:06:18:00112.36.36.70 24/may/2018:06:18:02110.36.38.70  24/may/2018:06:18:00

Result:

Welcome to use Nginx log analysis Gadget!!! Function One: Count the number of times each IP Access IP address: 123.168.71.118  number of visits: 1IP address: 110.36.38.70  Access Book Number: 1IP address:  153.119.226.180  number of Visits: 68IP address: 112.36.36.70  number of visits: 2IP address: 123.168.70.117  number of visits: 1IP Address:  123.168.70.113  number of Visits: 1IP address: 123.168.70.118  Access Book Number: 82IP address: 123.168.70.110  Number of visits: 1IP address: 112.36.36.74  number of visits: 2IP address: 103.106.120.4  number of visits: 42IP Address:  183.230.30.249   Number of Visits: 2 function Two: statistics only access 1 times IP address and time:ip:123.168.71.118  access time:24/may/2018:06:17:05ip:110.36.38.70  access time: 24/ may/2018:06:18:00ip:123.168.70.117  Access Time:24/may/2018:06:17:05ip:123.168.70.113  access time: 24/May/ 2018:06:16:59ip:123.168.70.110  Access time: 24/may/2018:06:17:04 function Three: Access multiple IP addresses, first time and last time: ip:153.119.226.180   First access time: 24/may/2018:06:13:15  last access time:24/may/2018:06:13:19ip:112.36.36.70  first access time:  24/may/2018:06:18:00  Last access time:24/may/2018:06:18:02ip:123.168.70.118  first time to visit:  24/may/ 2018:06:16:57  Last access time: 24/may/2018:06:17:43ip:112.36.36.74  first access time: 24/may/2018:06:18:00  last visited: 24/may/2018:06:18:00ip :103.106.120.4  first access time: 24/may/2018:06:12:10  last visited: 24/may/2018:06:12:12ip:183.230.30.249   First access time: 24/may/2018:06:21:56  last access time: 24/may/2018:06:21:56

Python parses the same IP first access time and last access time inside the Nginx log

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.