Skip to content

Instantly share code, notes, and snippets.

@aodin
Created May 31, 2022 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aodin/d86088f113cf1fffcc0aab8b3adc558c to your computer and use it in GitHub Desktop.
Save aodin/d86088f113cf1fffcc0aab8b3adc558c to your computer and use it in GitHub Desktop.
Parse nginx access.log and access.log.gz files into namedtuples
import csv
from collections import namedtuple
import gzip
from os import listdir
from os.path import join
Entry = namedtuple("Entry", "ip, blank, user, time, offset, request, status, sent, referer, agent")
def main(curdir="."):
entries = []
for f in listdir(curdir):
if not f.startswith("access.log"):
continue
opener = gzip.open if f.endswith(".gz") else open
full = join(curdir, f)
with opener(full, "rt") as f:
entries.extend(map(Entry._make, csv.reader(f, delimiter=" ")))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment