统计nginx日志的状态码

日志格式

 
   
  1. 61.159.140.123 - - [23/Aug/2014:00:01:42 +0800] "GET /favicon.ico HTTP/1.1" 404 \ "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 LBBROWSER" "-"
  2. 61.159.140.123 - - [23/Aug/2014:00:01:42 +0800] "GET /favicon.ico HTTP/1.1" 404 \ "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 LBBROWSER" "-"
 
   
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. with open('/Users/shaoying/Desktop/python/04/www_access_20140823.log') as f:
  4. accessDict = {}
  5. for oneAccess in f.readlines():
  6. oneAccessList = oneAccess.split(' ')
  7. accessDictKey = (oneAccessList[8],oneAccessList[6],oneAccessList[0])
  8. print accessDictKey
  9. if accessDictKey in accessDict:
  10. accessDict[accessDictKey] += 1
  11. else:
  12. accessDict[accessDictKey] = 1
  13. for k,v in accessDict.items():
  14. print [k[0],k[1],(k[2],v)]

转载于:https://www.cnblogs.com/caoxiaojian/p/8365690.html

你可能感兴趣的:(统计nginx日志的状态码)