Question: i keep having this error in my code here's my code: import re import datetime from csv import DictReader, DictWriter class LogEntry: def _ _
i keep having this error in my code here's my code:
import re
import datetime
from csv import DictReader, DictWriter
class LogEntry:
def initself eventtime, internalip portnumber, protocol, action, ruleid sourceip country, countryname:
self.eventtime LogEntry.parsedatetimeeventtime
self.internalip internalip
self.portnumber portnumber
self.protocol protocol
self.action action
self.ruleid ruleid
self.sourceip sourceip
self.country country
self.countryname countryname
@staticmethod
def parsedatetimetimestamp:
try:
return datetime.datetime.strptimetimestampYmd H:M:S Z
except ValueError:
raise ValueErrorInvalid timestamp format"
@property
def ipvclassself:
octets self.sourceipsplit
if lenoctets:
return None
octetvalue intoctets
if octetvalue :
return A
elif octetvalue :
return B
elif octetvalue :
return C
else:
return None
@staticmethod
def countrycountlogentries, countrycode:
filteredenteries entry for entry in logentries if entry.country.lower countrycode.lower
count lenfilteredenteries
printfNumber of entries from countrycode.upper: count
@staticmethod
def parselogentries, args, month:
filteredentries entry for entry in logentries if entry.eventtime.month month
if filteredentries:
outputfilename fargsfilename.splitargsmonthlogscsv
try:
with openoutputfilename, modew newline as csvfile:
fieldnames logentriesdictkeys
writer DictWritercsvfile fieldnamesfieldnames
writer.writeheader
for entry in filteredentries:
writer.writerowentrydict
printfFiltered log entries exported to outputfilename
except FileNotFoundError:
printfError: File outputfilename not found."
except PermissionError:
printfError: Insufficient permissions to write to outputfilename
else:
printNo entries found for the given month."
index.py
from loganalyzer import LogEntry
import argparse
import csv
import pytz
def parseargs:
parser argparse.ArgumentParserdescription"Accept a CSV file of firewall log data and prepare it for analysis"
parser.addargumentfilename", f requiredTrue, help"Filename"
parser.addargumentaction", a requiredTrue, help"Execute an action on the CSV file valid values are 'head', 'deny', 'source', and 'parse'
parser.addargumentcountrycode", c helpletter country code for 'source' action"
parser.addargumentmonth", m typeint, help"Month for the 'parse' action"
return parser.parseargs
def printheadlogentries:
if lenlogentries:
printFirst entries:"
for i in range:
printflogentriesi
else:
printAll entries:"
for entry in logentries:
printentry
def denycountlogentries:
deniedentries entry for entry in logentries if entry.action "Deny"
count lendeniedentries
printfNumber of denied entries: count
def main:
args parseargs
filename args.filename
logentries
filename "firewalllogssample.csv
with openfilenamer newline as csvfile:
reader csvDictReadercsvfile
for row in reader:
logentry LogEntryroweventtime' rowinternalip rowportnumber' rowprotocol rowaction rowruleid rowsourceip rowcountry rowcountryname'
logentries.appendlogentry
for logentry in logentries::
timezone pytztimezoneUTC
formattedtime logentry.eventtime.astimezonetimezonestrftimeYmd H:M:Sz
printfformattedtimelogentry.actionlogentry.sourceiplogentry.ipvclasslogentry.countryname
if args.action "head":
printheadlogentries
elif args.action "deny":
denycountlogentries
elif args.action 'source' and args.country:
countrycount LogEntry.countrycountlogentries, args.country
elif args.action "parse":
if args.month and args.month :
filteredentries LogEntry.parselogentries, args.month
and here's the error i keep having:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
