Question: Create a new file named log _ analyzer.py in the directory you created for this application. This file will be a module that you will

Create a new file named log_analyzer.py in the directory you created for this application. This file will be a module that you will include into your main directive code.
In the log_analyzer.py file, create a new Python class named LogEntry. Each object of class LogEntry will represent one line in the CSV file.
The LogEntry class will have the following attributes: event_time, internal_ip, port_number, protocol, action, rule_id, source_ip, country, and country_name. The other fields are optional.
The CSV file contains a string with the date and time information. In the initializer for the LogEntry class, use the Python datetime package to ensure that the value for event_date is stored as a datetime object. (This will be important later.)
Add a property to the LogEntry class called ipv4_class that uses a regular expression to examine the source_ip and returns the string value of the IPv4 address class (which will be either A,B,C, or D) based on the public IP address range. For example, the source_ip 11.177.69.220 will return A and the source_ip 173.205.219.112 will return C.
Think: How can you find just the first component of an IPv4 address?
Write Unit Tests for the LogEntry Class
Create a new file named test_log_analyzer.py in the directory you created for this application. Write several unit tests that evaluate the following functionality of your LogEntry class:
1. Ensure that the event_time string is properly converted to a datetime object.
a. For example, you could create a new instance of LogEntry with a string value of 2022-01-0108:29:25 UTC for the event_time. You could then assert that the month of the resulting datetime object ==1.
b. You could also assert that the hour of the resulting datetime object ==8.
2. Ensure that the ipv4_class returns the correct IPv4 address class
a. For example, you can assert that the ipv4_class ==D for the source_ip 229.163.4.51
Write several tests (asserts) for each of the above scenarios to ensure your class is behaving correctly. Make sure all your tests pass before moving on.
THINK: What else could go wrong when converting the CSV to objects of class LogEntry?

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!