Objectives: The purpose of this homework assignment is to demonstrate your abili
ID: 3866766 • Letter: O
Question
Objectives:
The purpose of this homework assignment is to demonstrate your ability to apply what you have learned about dictionaries and CSV files.
Instructions:
Attached is a CSV file that contains rows of data describing the attack information from a particular IP address. The first column is the IP address, the second the country of origin, the third the number of attack attempts and the fourth represents the number of unique user names used in the attacks. For example, IP address 222.234.0.57 is in the Republic of Korea and attempted to log in 599 times using 580 invalid user names. Apparently some of the user names were used more than once.
Your objective is to open the file, read it line by line, split the data into individual variables and assign it to three different dictionaries. The key for each dictionary is the IP address. Each dictionary contains different values that can be associated with the key (IP). For example, use the IP to look up the coutry of origin in one dictionary, the number of attacks in another and the number of unique user names in the third dictionary.
Provide a menu that allows the user to select what it is they would like to do. They can:
Find the number of attacks associated with an IP address
Find the country of origin for an IP
Find the number of different invalid user names tried
Quit
For any of the choices (except Quit), the user enters the IP address. If the address does not match any in the data, simply display a message that there is no record of an attack attempt from that IP.
Here is the CSV file:
IP,Country,Count,# of user names
222.234.0.57,Republic of Korea,599,580
101.44.1.135,China,116,90
79.142.68.127,the Netherlands,2,1
125.210.221.161,China,13,10
183.61.135.236,China,1,1
211.155.224.203,China,55,55
188.95.234.6,Germany,3,3
85.233.66.158,Russian Federation,1,1
210.14.71.201,China,15,13
31.3.231.229,the United Kingdom,1,1
88.191.133.24,France,39,39
58.240.17.250,China,4,4
59.44.157.74,China,14,1
Extra credit: Since the first line of the CSV file is a column heading, the first row of data is not valid. "IP" will be added as a key to each of the tables with the other column headings showing as values. No credit will be deducted for this behaviour. However, if you find a way to ignore the first line of data when you read the file, you'll be awarded up to 10 points extra credit, depending upon the quality of your solution.
Explanation / Answer
#!usr/bin/python
dict1 = {}
dict2 = {}
dict3 = {}
with open("input19.txt") as f: #Assuming input file is input19.txt
for line in f:
if line.split(',')[0] == "IP":
continue;
ip = line.split(',')[0]
country = line.split(',')[1]
num_attacks = line.split(',')[2]
num_users = line.split(',')[3]
dict1[ip] = country
dict2[ip] = num_attacks
dict3[ip] = num_users
choice = 1
while choice != 4:
print("1.Find the number of attacks")
print("2.Find the country of origin")
print("3.Find the number of invalid users")
print("4.Quit")
choice = int(input("Enter your choice:"))
if choice == 1:
inp = input("Enter IP:")
if dict2.get(inp) != None:
print("Number of attacks:",dict2.get(inp))
else:
print("No record for attack attempt from ",inp)
else:
if choice == 2:
inp = input("Enter IP:")
if dict1.get(inp) != None:
print("Country of origin: ",dict1.get(inp))
else:
print("No record for attack attempt from ",inp)
else:
if choice == 3:
inp = input("Enter IP:")
if dict3.get(inp) != None:
print("Number of invalid users:",dict3.get(inp))
else:
print("No record for attack attempt from ",inp)
print(" ")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.