Question: I have this code and i am trying to fix it so that the output of the time will be in the format YYYY

I have this code and i am trying to fix it so that the output of the time will be in the format "YYYY-MM-DD HH:MM:SS\pm HHMM", which includes the timezone offset.
here" is my code
log_analyzer.py
import re
from datetime import datetime
import csv
class LogEntry:
def __init__(self, event_time, internal_ip, port_number, protocol, action, rule_id, source_ip, country, country_name):
self.event_time = datetime.strptime(event_time, '%Y-%m-%d %H:%M:%S %Z')
self.internal_ip = internal_ip
self.port_number = port_number
self.protocol = protocol
self.action = action
self.rule_id = rule_id
self.source_ip = source_ip
self.country = country
self.country_name = country_name
@property
def ipv4_class(self):
first_octet = int(self.source_ip.split('.')[0])
if 1<= first_octet <=126:
return 'A'
elif 128<= first_octet <=191:
return 'B'
elif 192<= first_octet <=223:
return 'C'
elif 224<= first_octet <=239:
return 'D'
else:
return 'Unknown'
test_log_analyzer.py
import unittest
from log_analyzer import LogEntry
class TestLogEntry(unittest.TestCase):
def test_event_time_conversion(self):
log_entry = LogEntry(event_time ="2022-01-0108:29:25 UTC", internal_ip ="192.168.1.1", port_number ="22", protocol ="TCP",
action = "ALLOW", rule_id ="1001", source_ip ="192.168.1.100", country ="US", country_name = "United States")
self.assertEqual(log_entry.event_time.month, 1)
self.assertEqual(log_entry.event_time.hour, 8)
def test_ipv4_class(self):
log_entry_a = LogEntry("2022-01-0108:29:25 UTC", "192.168.1.1","22","TCP", "ALLOW", "1001","10.0.0.1","US", "United States")
log_entry_b = LogEntry("2022-01-0108:29:25 UTC", "192.168.1.1","22","TCP", "ALLOW", "1001","128.0.0.1","US", "United States")
log_entry_c = LogEntry("2022-01-0108:29:25 UTC", "192.168.1.1","22","TCP", "ALLOW", "1001","192.0.2.1","US", "United States")
log_entry_d = LogEntry("2022-01-0108:29:25 UTC", "192.168.1.1","22","TCP", "ALLOW", "1001","229.163.4.51","US", "United States")
self.assertEqual(log_entry_a.ipv4_class, 'A')
self.assertEqual(log_entry_b.ipv4_class, 'B')
self.assertEqual(log_entry_c.ipv4_class, 'C')
self.assertEqual(log_entry_d.ipv4_class, 'D')
if __name__=='__main__':
unittest.main()
index.py
from log_analyzer import LogEntry
import argparse
import csv
import pytz
def parse_args():
parser = argparse.ArgumentParser(description="Analyze firewall log CSV files.")
parser.add_argument("--filename", type=str, help="The filename of the CSV file containing the log data.")
return parser.parse_args()
def main():
args = parse_args()
filename = args.filename
log_entries =[]
with open(filename, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
log_entry = LogEntry(row['event_time'], row['internal_ip'], row['port_number'], row['protocol'], row['action'], row['rule_id'], row['source_ip'], row['country'], row['country_name'])
log_entries.append(log_entry)
for log_entry in log_entries[:5]:
print(f"{log_entry.event_time.strftime('%Y-%m-%d %H:%M:%S')},{log_entry.action},{log_entry.source_ip},{log_entry.ipv4_class},{log_entry.country_name}")
if __name__=="__main__":
main()
below is the csv file and the wrong output in the terminal
firewall_logs_sample.cSv > data
event_time,internal_ip, port_number, protocol, action, rule_id, rule_description, as_domain, as_name, asn, continent, continent_name, country,
2022-01-0100:18:38 UTC,10.248.203.131,20,FTP - Data, Allow,186, Allow traffic for mobile device management (MDM) services,greenhouse!
2022-01-0100:32:27 UTC,172.27.117.35,25,SMTP - Simple Mail Transfer Protocol, Deny,159,Block traffic for known buffer overflow comm
2022-01-0100:52:21 UTC,10.144.77.12,993,IMAPS - Encrypted IMAP, Allow, 308, Allow traffic for remote desktop support tools, hostiservel
2022-01-0100:57:07 UTC,10.128.66.93,137,NetBIOS Name Service, Deny,85, Block traffic for known XSS payloads,
cloudflare.com, "cloudfla
2022-01-0101:15:17 UTC,192.168.75.62,143, IMAP - Internet Message Access Protocol,Allow,64,Allow traffic for certificate revocation
2022-01-0101:25:52 UTC,10.169.15.156,21,FTP - Control, Allow, 306,Allow traffic for network printer discovery,
cybera.ca, Cybera Inc, A:
2022-01-0101:41:58 UTC,192.168.222.75,1194,OpenVPN, Log,359, Log LDAP traffic for directory services, gtt. net, GTT Communications Neth
2022-01-0101:52:45 UTC,192.168.104.177,636,LDAPS - Encrypted LDAP, Warning,457, warn on repeated RDP connection attempts to specific
2022-01-0102:11:36 UTC,172.24.85.243,143,IMAP - Internet Message Access Protocol,Allow,166,Allow traffic for network backup and re

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!