统计nginx日志的状态码
日志格式
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" "-"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" "-"
#!/usr/bin/env python# -*- coding:utf-8 -*-with open('/Users/shaoying/Desktop/python/04/www_access_20140823.log') as f:accessDict = {}for oneAccess in f.readlines():oneAccessList = oneAccess.split(' ')accessDictKey = (oneAccessList[8],oneAccessList[6],oneAccessList[0])print accessDictKeyif accessDictKey in accessDict:accessDict[accessDictKey] += 1else:accessDict[accessDictKey] = 1for k,v in accessDict.items():print [k[0],k[1],(k[2],v)]
作者:曹小贱
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

浙公网安备 33010602011771号