Question: HERE IS MY PROBLEM THAT I NEED CODED IN PYTHON Your manager wants you to write a script that looks for all the error codes
HERE IS MY PROBLEM THAT I NEED CODED IN PYTHON
Your manager wants you to write a script that looks for all the error codes in a logfile and then writes those errors out to individual error files so that they can be handled by the appropriate IT support team.
You will use be using the text file logfile.txt (see attached). This file is a server log file that contains the recorded events taking place in a web server. The content of the file is in a special format known as common log format. According to Wikipedia:
The Common Log Format is a standardized text file format used by web servers when generating server log files.
Heres an example of a log record:
'pd9049dac.dip.t-dialin.net - - [01/May/2001:01:51:25 -0700] "GET /accesswatch/accesswatch-1.33/ HTTP/1.0" 200 1004'
A "-" in a field indicates missing data.
pd9049dac.dip.t-dialin.net is the IP address of the client (remote host) which made the request to the server.
[01/May/2001:01:51:25 -0700] is the date, time, and time zone that the request was received, by default in strftime format%d/%b/%Y:%H:%M:%S %z.
"GET /accesswatch/accesswatch-1.33/ HTTP/1.0" is the request line from the client.
The method GET, /accesswatch/accesswatch-1.33/ is the resource requested, and HTTP/1.0 is the HTTP protocol.
200 is the HTTP status code returned to the client.
2xx is a successful response
3xx a redirection
4xx a client error, and
5xx a server error
1004 is the size of the object returned to the client, measured in bytes.
Because client errors and server errors are managed by different teams, you are charged with finding all the records with HTTP status code errors of 4xx or 5xx and write the status code and the URL portion of the request to the appropriate error file to make it easier for each team to investigate the files that are causing the errors. Client error information is written a file called Client_Errors.txt and server error information is written to a file called Server_Errors.txt.
Example:
The data within the Client_Errors.txt file would look something like this:
Status Code: 400 File: x Status Code: 403 File: /testing/book1.html Status Code: 404 File: /robots.txt Status Code: 404 File: /testing/www.approximity.com Status Code: 404 File: /testing/www.xprogramming.org
Lastly, to best aid the support teams, you will make sure that your files do not contain any duplicate records. (Hint: you may want to look ahead at how to use sets).
** You may only use tools and techniques that we covered in class. You cannot use tools, methods, keyword, etc. from sources outside of what is covered in class.
HERE IS MY CODE:
I WANT TO MAKE SURE I FOLLOWED THE DIRECTIONS CORRECTLY
file1 = open(r"C:\Scripting2\logfile.txt", 'r') client_codes = [] server_codes = [] client_set = set() server_set = set()
for i in file1: content1 = file1.readline() word_list = content1.split()
if 400 <= int(word_list[8]) < 500: client_codes.append(word_list)
else: if 500 <= int(word_list[8]) < 600: server_codes.append(word_list)
num = 0 for i in client_codes: code1 = f"Status Code: {client_codes[num][8]} File: {client_codes[num][6]} " client_set.add(code1) num += 1
num2 = 0 for i in server_codes: code1 = f"Status Code: {server_codes[num2][8]} File: {server_codes[num2][6]} " server_set.add(code1) num2 += 1
sorted_clients = list(client_set) sorted_clients.sort()
file2 = open("client_errors.txt", 'w') file3 = open("server_errors.txt", 'w')
for i in sorted_clients: a = i file2.write(a)
for i in server_set: file3.write(i)
# print(client_set)
print("done") file2.close file3.close file1.close
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
