Python scripts for analyzing Apache logs

Source: Internet
Author: User
Tags apache log

The core regular modules were taken directly from the Apachelog module, and I almost didn't do anything.
Apahelog Module Address: http://pypi.python.org/pypi/apachelog/1.0 #!/usr/bin/env python #coding =gbk "" "Apache Log Parser, Module Apahcelog:     http://pypi.python.org/pypi/apachelog/1.0 "" "Import re Import sys Import  configparser   Def getformat ():     # get ' format ' from Format.ini     config = Config Parser.configparser ()     Try:         INIFH = open (' Format.ini ')     except  IOError:         print  "Can not load Format.ini!"         Sys.exit (1)     CONFIG.READFP (INIFH)     FormatName = "    format = {}     try:     &NBSP ;   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 O ption [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 ():     & nbsp   DEF __INIT__ (Self, format):         self._names = []         Self._regex = None     & nbsp   Self._pattern = "        Self._parse_format (format)         Def _pars E_format (self, format):         format = Format.strip ()         format = re.sub (' [ /t]+ ', '   ', format}                 subpatterns = []       &NBS P   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           &N Bsp   If hasquotes:                 element = Lstripquotes.sub (", Element) & nbsp               element = Rstripquotes.sub (", Element)                         self._names.append (Self.alias (Element))         &NBS P               Subpattern = ' (/s*) '               &NB Sp         if hasquotes:                 If element = = '%r ' or findreferreragent.search (element):                     Subpatter n = R '/' ([^]//]* (?:/ /. [^ "//]*" *)/"'                 ELSE:         &NBsp           Subpattern = R '/"([^/"]*)/"'                 &N Bsp           Elif findpercent.search (Element):                 Subpattern = R ' (/[[^/]]+/]) '                         & nbsp   Elif element = = '%u ':                 Subpattern = ' (. +?) '   &nbs P                     Subpatterns.append (subpattern)                 Self._pattern = ' ^ ' + '   '. Join (Subpatterns) + ' $ '         Try:             Self._regex = Re.compile (self._pattern)         EXCEPT&NBSP ; Exception, E:             Raise apachelogparsererror (e)         &NB Sp  Def parse (self, line):         line = Line.strip ()         match = sel F._regex.match (line)                 If match:         &N Bsp   data = {}             for k, V in Zip (self._names, match.groups

Related Article

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.