Question
1. Download the following python program and test log file. parse_logs.pyDownload parse_logs.py test_log.log Download test_log.log 2. Review the python code from parse_logs.py and run the
1. Download the following python program and test log file. parse_logs.pyDownload parse_logs.py test_log.log Download test_log.log
2. Review the python code from parse_logs.py and run the program to see what it does. Compare the output to the test_log.log file.
3. In a Word Processor answer the following questions. A) Explain what this code is doing? What is it filtering for? B) Describe what programming concepts are being used in this program. C) Modify the python code to filter for some other aspect of the test_log.log file. Explain what changes you made to the file and what code you changed.
4. Save the your document.
5. Submit your document and your modified parse_logs.py code.
1. Download the following python program and test log file. parse_logs.pyDownload parse_logs.py test_log.log Download test_log.log
2. Review the python code from parse_logs.py and run the program to see what it does. Compare the output to the test_log.log file.
3. In a Word Processor answer the following questions. A) Explain what this code is doing? What is it filtering for? B) Describe what programming concepts are being used in this program. C) Modify the python code to filter for some other aspect of the test_log.log file. Explain what changes you made to the file and what code you changed.
4. Save the your document.
5. Submit your document and your modified parse_logs.py code.
_____________________________________________________________________________________
import os import re
# Regex used to match relevant loglines (in this case, a specific IP address) line_regex = re.compile(r".*fwd=\"12.34.56.78\".*$")
# Output file, where the matched loglines will be copied to output_filename = os.path.normpath("parsed_lines.log") # Overwrites the file, ensure we're starting out with a blank file with open(output_filename, "w") as out_file: out_file.write("")
# Open output file in 'append' mode with open(output_filename, "a") as out_file: # Open input file in 'read' mode with open("test_log.log", "r") as in_file: # Loop over each log line for line in in_file: # If log line matches our regex, print to console, and output file if (line_regex.search(line)): print(line) out_file.write(line)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started