Skip to content

Commit 2ce8300

Browse files
authored
Create logparser.py
1 parent 39fa344 commit 2ce8300

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

logparser.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3
2+
3+
4+
5+
import re
6+
7+
8+
regex_host = r'(?P<host>.*?)'
9+
regex_identity = r'(?P<identity>\S+)'
10+
regex_user = r'(?P<user>\S+)'
11+
regex_time = r'\[(?P<time>.*?)\]'
12+
regex_request = r'\"(?P<request>.*?)\"'
13+
regex_status = r'(?P<status>\d{3})'
14+
regex_size = r'(?P<size>\S+)'
15+
regex_referer = r'\"(?P<referer>.*?)\"'
16+
regex_agent = r'\"(?P<agent>.*?)\"'
17+
regex_space = r'\s'
18+
19+
pattern = regex_host + regex_space + regex_identity + regex_space + \
20+
regex_user + regex_space + regex_time + regex_space + \
21+
regex_request + regex_space + regex_status + regex_space + \
22+
regex_size + regex_space + regex_referer + regex_space + \
23+
regex_agent
24+
25+
26+
def parser(s):
27+
"""
28+
return type : dict()
29+
return format: {
30+
host:str , identity:str , user:str ,
31+
time:str ,request:str , status:str ,
32+
size:str , referer:str, agent:str
33+
}
34+
returns None if failed.
35+
"""
36+
try:
37+
parts = re.match(pattern,s)
38+
return parts.groupdict()
39+
except Exception as err:
40+
print(err)

0 commit comments

Comments
 (0)