Created
May 31, 2022 20:17
-
-
Save aodin/d86088f113cf1fffcc0aab8b3adc558c to your computer and use it in GitHub Desktop.
Parse nginx access.log and access.log.gz files into namedtuples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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