: This article mainly introduces how to count the ip traffic of the client based on nginx access logs in python. if you are interested in the PHP Tutorial, refer to it. Professional statistical websites, such as Baidu Statistics, Google Analytics, and cnzz, provide common statistical indicators for webmasters, such as uv, pv, online duration, and ip address, in addition, due to network reasons, I found that Google Analytics would count hundreds more ip addresses than Baidu, so I want to write my own scripts to learn the actual traffic volume, however, nginx-based access logs are much more than those in the statistics background, because many spider visits are also included in statistics, as well as static file statistics, in fact, if the algorithm is improved, the useless statistics can be completely filtered out. today we will share the most basic statistics with Niu, and we will also learn and review the python language.
For example, the nginx log on the server is as follows:
221.221.155.54-[02/Aug/2014: 15: 16: 11 + 0800] "GET/HTTP/1.1" 200 8482 "http://www.zuidaima.com/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36 ""-"" 0.020"
221.221.155.53--[02/Aug/2014: 15: 16: 11 + 0800] "GET/HTTP/1.1" 200 8482 "http://www.zuidaima.com/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36 ""-"" 0.020"
221.221.155.54-[02/Aug/2014: 15: 16: 11 + 0800] "GET/HTTP/1.1" 200 8482 "http://www.zuidaima.com/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36 ""-"" 0.020"
The statistical script is as follows:
Stat_ip.py
# Encoding = utf8
Import re
Zuidaima_nginx_log_path = "/usr/local/nginx/logs/www.zuidaima.com. access. log"
Pattern = re. compile (r' ^ \ d {1, 3} \. \ d {1, 3} \. \ d {1, 3} \. \ d {1, 3 }')
Def stat_ip_views (log_path ):
Ret = {}
F = open (log_path, "r ")
For line in f:
Match = pattern. match (line)
If match:
Ip = match. group (0)
If ip in ret:
Views = ret [ip]
Else:
Views = 0
Views = views + 1
Ret [ip] = views
Return ret
Def run ():
Ip_views = stat_ip_views (zuidaima_nginx_log_path)
Max_ip_view = {}
For ip in ip_views:
Views = ip_views [ip]
If len (max_ip_view) = 0:
Max_ip_view [ip] = views
Else:
_ Ip = max_ip_view.keys () [0]
_ Views = max_ip_view [_ ip]
If views> _ views:
Max_ip_view [ip] = views
Max_ip_view.pop (_ ip)
Print "ip:", ip, ", views:", views
# Total number of ip addresses
Print "total:", len (ip_views)
# Maximum Access ip address
Print "max_ip_view:", max_ip_view
Run ()
The running result is as follows:
Ip: 221.221.155.53, views: 1
Ip: 221.221.155.54, views: 2
Total: 2
Max_ip_view: {'192. 221.155.54 ': 2}
In this way, the access traffic of all ip addresses and the access of the largest ip address are obtained.
The above describes how to count the ip access volume of the client based on nginx access logs in python, including some content. if you are interested in the PHP Tutorial.