Program is done in Python 3.1 Attached is a CSV file that contains rows of data
ID: 3866625 • Letter: P
Question
Program is done in Python 3.1
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.
cv file is an excel file with Ip addresses
Explanation / Answer
#!usr/bin/python
dict1 = {}
dict2 = {}
dict3 = {}
with open("input19.txt") as f: #Assuming input file is input19.txt
for line in f:
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.