python分析apache日誌的指令碼

來源:互聯網
上載者:User

核心的正則模組是直接從apachelog模組裡拿出來的,我自己幾乎什麼都沒做
apahelog模組地址:http://pypi.python.org/pypi/apachelog/1.0 #!/usr/bin/env python #coding=gbk """ Apache Log Parser, see module apahcelog:     http://pypi.python.org/pypi/apachelog/1.0 """ import re import sys import ConfigParser   def getFormat():     # get ‘format’ from format.ini     config = ConfigParser.ConfigParser()     try:         inifh = open(‘format.ini‘)     except IOError:         print "Can not load format.ini !"         sys.exit(1)     config.readfp(inifh)     formatName = ”     format = {}     try:         formatName = config.get(‘type‘,‘name‘)     except ConfigParser.NoOptionError:         print "Bad section/option name! Please check format.ini"         sys.exit(1)     try:         options = config.options(‘format‘)     except ConfigParser.NoOptionError:         print "Can not enum option [format]"         sys.exit(1)     for opt in options:         if opt not in format.keys():             format[opt] = config.get(‘format‘,opt)         else:             print "duplicate name in option [format]"             sys.exit(1)     try:         ret = format[formatName]     except KeyError:         print "Format /"%s/" has not been defined in format.ini" %formatName         sys.exit(1)     return ret # intercept from module apachelog class ApacheLogParserError(Exception):     pass class parser():         def __init__(self, format):         self._names = []         self._regex = None         self._pattern = ”         self._parse_format(format)         def _parse_format(self, format):         format = format.strip()         format = re.sub(‘[ /t]+‘,‘ ‘,format)                 subpatterns = []           findquotes = re.compile(r‘^//"‘)         findreferreragent = re.compile(‘Referer|User-Agent‘)         findpercent = re.compile(‘^%.*t$‘)         lstripquotes = re.compile(r‘^//"‘)         rstripquotes = re.compile(r‘//"$‘)                         for element in format.split(‘ ‘):               hasquotes = 0             if findquotes.search(element): hasquotes = 1               if hasquotes:                 element = lstripquotes.sub(”, element)                 element = rstripquotes.sub(”, element)                         self._names.append(self.alias(element))                         subpattern = ‘(/S*)‘                         if hasquotes:                 if element == ‘%r‘ or findreferreragent.search(element):                     subpattern = r‘/"([^"//]*(?://.[^"//]*)*)/"‘                 else:                     subpattern = r‘/"([^/"]*)/"‘                             elif findpercent.search(element):                 subpattern = r‘(/[[^/]]+/])‘                             elif element == ‘%U‘:                 subpattern = ‘(.+?)‘                         subpatterns.append(subpattern)                 self._pattern = ‘^‘ + ‘ ‘.join(subpatterns) + ‘$‘         try:             self._regex = re.compile(self._pattern)         except Exception, e:             raise ApacheLogParserError(e)             def parse(self, line):         line = line.strip()         match = self._regex.match(line)                 if match:             data = {}             for k, v in zip(self._names, match.groups

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.