From 5badf07587581036b5c19c32e64e30ff55a30fdc Mon Sep 17 00:00:00 2001 From: Dan Capper Date: Thu, 9 May 2024 11:37:19 +1200 Subject: [PATCH 1/2] Update apache-fake-log-gen.py fix "RuntimeError: generator raised StopIteration" error on python 3.7 due to PEP 479 enforcement (https://docs.python.org/3/whatsnew/3.7.html#changes-in-python-behavior) --- apache-fake-log-gen.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apache-fake-log-gen.py b/apache-fake-log-gen.py index fe4784f..3a3b065 100644 --- a/apache-fake-log-gen.py +++ b/apache-fake-log-gen.py @@ -25,8 +25,10 @@ def __init__(self, value): def __iter__(self): """Return the match method once, then stop""" - yield self.match - raise StopIteration + try: + yield self.match + except StopIteration: + return def match(self, *args): """Indicate whether or not to enter a case suite""" From f4c46e7d4c4f5904b9fd3d732ce4ec17d7b34e23 Mon Sep 17 00:00:00 2001 From: Dan Capper Date: Thu, 9 May 2024 13:32:46 +1200 Subject: [PATCH 2/2] Prevent backtrace/error on KeyboardInterrupt in main loop --- apache-fake-log-gen.py | 61 ++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/apache-fake-log-gen.py b/apache-fake-log-gen.py index 3a3b065..afdb762 100644 --- a/apache-fake-log-gen.py +++ b/apache-fake-log-gen.py @@ -82,32 +82,35 @@ def match(self, *args): flag = True while (flag): - if args.sleep: - increment = datetime.timedelta(seconds=args.sleep) - else: - increment = datetime.timedelta(seconds=random.randint(30, 300)) - otime += increment - - ip = faker.ipv4() - dt = otime.strftime('%d/%b/%Y:%H:%M:%S') - tz = datetime.datetime.now(local).strftime('%z') - vrb = numpy.random.choice(verb,p=[0.6,0.1,0.1,0.2]) - - uri = random.choice(resources) - if uri.find("apps")>0: - uri += str(random.randint(1000,10000)) - - resp = numpy.random.choice(response,p=[0.9,0.04,0.02,0.04]) - byt = int(random.gauss(5000,50)) - referer = faker.uri() - useragent = numpy.random.choice(ualist,p=[0.5,0.3,0.1,0.05,0.05] )() - if log_format == "CLF": - f.write('%s - - [%s %s] "%s %s HTTP/1.0" %s %s\n' % (ip,dt,tz,vrb,uri,resp,byt)) - elif log_format == "ELF": - f.write('%s - - [%s %s] "%s %s HTTP/1.0" %s %s "%s" "%s"\n' % (ip,dt,tz,vrb,uri,resp,byt,referer,useragent)) - f.flush() - - log_lines = log_lines - 1 - flag = False if log_lines == 0 else True - if args.sleep: - time.sleep(args.sleep) + try: + if args.sleep: + increment = datetime.timedelta(seconds=args.sleep) + else: + increment = datetime.timedelta(seconds=random.randint(30, 300)) + otime += increment + + ip = faker.ipv4() + dt = otime.strftime('%d/%b/%Y:%H:%M:%S') + tz = datetime.datetime.now(local).strftime('%z') + vrb = numpy.random.choice(verb,p=[0.6,0.1,0.1,0.2]) + + uri = random.choice(resources) + if uri.find("apps")>0: + uri += str(random.randint(1000,10000)) + + resp = numpy.random.choice(response,p=[0.9,0.04,0.02,0.04]) + byt = int(random.gauss(5000,50)) + referer = faker.uri() + useragent = numpy.random.choice(ualist,p=[0.5,0.3,0.1,0.05,0.05] )() + if log_format == "CLF": + f.write('%s - - [%s %s] "%s %s HTTP/1.0" %s %s\n' % (ip,dt,tz,vrb,uri,resp,byt)) + elif log_format == "ELF": + f.write('%s - - [%s %s] "%s %s HTTP/1.0" %s %s "%s" "%s"\n' % (ip,dt,tz,vrb,uri,resp,byt,referer,useragent)) + f.flush() + + log_lines = log_lines - 1 + flag = False if log_lines == 0 else True + if args.sleep: + time.sleep(args.sleep) + except KeyboardInterrupt: + sys.exit(0)