From 85ddda557bce0b3db491f06c16c28c2ea20cb3fe Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 18 Feb 2012 15:15:46 -0500 Subject: [PATCH] Add apachelog.processor.status.StatusProcessor. --- apachelog/processor/status.py | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 apachelog/processor/status.py diff --git a/apachelog/processor/status.py b/apachelog/processor/status.py new file mode 100644 index 0000000..420b161 --- /dev/null +++ b/apachelog/processor/status.py @@ -0,0 +1,43 @@ +from . import Processor as _Processor + + +class StatusProcessor (_Processor): + r"""Track the status of requested files. + + >>> import StringIO + >>> from apachelog.parser import Parser, FORMATS + >>> from apachelog.processor import process + >>> stream = StringIO.StringIO('\n'.join([ + ... '192.168.0.1 - - [18/Feb/2012:10:25:43 -0500] "GET / HTTP/1.1" 200 560 "-" "Mozilla/5.0 (...)"', + ... '192.168.0.1 - - [18/Feb/2012:10:25:43 -0500] "GET /style.css HTTP/1.1" 200 8240 "-" "Mozilla/5.0 (...)"', + ... '192.168.0.2 - - [18/Feb/2012:10:25:58 -0500] "GET / HTTP/1.1" 404 560 "-" "Mozilla/5.0 (...)"', + ... ])) + >>> parser = Parser(FORMATS['extended']) + >>> sp = StatusProcessor() + >>> process(stream, parser, [sp]) + >>> for request,status in sorted(sp.request.items()): + ... print('\t'.join([request, ', '.join(sorted(status))])) + ... # doctest: +NORMALIZE_WHITESPACE + GET / HTTP/1.1 200, 404 + GET /style.css HTTP/1.1 200 + >>> for status,request in sorted(sp.status.items()): + ... print('\t'.join([status, ', '.join(sorted(request))])) + ... # doctest: +NORMALIZE_WHITESPACE + 200 GET / HTTP/1.1, GET /style.css HTTP/1.1 + 404 GET / HTTP/1.1 + """ + def __init__(self): + self.request = {} + self.status = {} + + def process(self, data): + request = data['%r'] + status = data['%>s'] + if request in self.request: + self.request[request].add(status) + else: + self.request[request] = set([status]) + if status in self.status: + self.status[status].add(request) + else: + self.status[status] = set([request]) -- 2.26.2