Skip to content

Instantly share code, notes, and snippets.

@randy-ran
Forked from shenfeng/nginx_access_log_parser.py
Last active November 21, 2017 09:04
Show Gist options
  • Save randy-ran/9b6cc1c8fff016189a62abce14dce276 to your computer and use it in GitHub Desktop.
Save randy-ran/9b6cc1c8fff016189a62abce14dce276 to your computer and use it in GitHub Desktop.
[parse nginx access log in python] parse nginx access log in python #log
def seg_access_log(line):
delimiters = {'[': ']', '"': '"'}
idx, start, count, delimiter, results = 0, 0, len(line), ' ', []
while 1:
idx = line.find(delimiter, start)
delimiter = ' ' # reset
if idx < 0:
break
if start < idx:
results.append(line[start:idx])
start = idx + 1
# if idx != count - 1 and line[idx + 1] in delimiters:
if line[idx + 1] in delimiters:
delimiter = delimiters[line[idx + 1]]
start += 1
if start < count:
results.append(line[start:].rstrip())
return results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment