Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests, json, sys
- from time import sleep
- from collections import Counter
- ip_addr = []
- countries = []
- api_key = "b8c3cc5bb9e845"
- def getIP():
- import socket, csv
- with open('JAR-16-20296A.csv') as csv_file:
- reader = csv.DictReader(csv_file)
- for row in reader:
- line = [row['INDICATOR_VALUE']]
- line = [item.replace("[.]", ".") for item in line]
- try:
- socket.inet_aton(line[0])
- ip_addr.append(line[0])
- except socket.error:
- continue
- return 0
- def printList(list):
- for item in list:
- print(item)
- def update_progress(job_title, progress):
- length = 20
- block = int(round(length*progress))
- msg = "\r{0}: [{1}] {2}%".format(job_title, "#"*block + "-"*(length-block), round(progress*100, 2))
- if progress >= 1: msg += " DONE\r\n"
- sys.stdout.write(msg)
- sys.stdout.flush()
- def getCountries():
- i = 0
- x = 100/len(ip_addr)
- for ip in ip_addr:
- url = "https://www.ipinfo.io/" + ip + "/geo/" + "?token=" + api_key
- r = requests.get(url, verify=True)
- if r.status_code == 200:
- data = json.loads(r.content)
- countries.append(data['country'])
- update_progress("Progress", i/100.0)
- i = i + x
- elif r.status_code == 429:
- print("Request limit reached")
- return
- else:
- print("Other HTTP error...")
- def getTop(in_list, number):
- c = Counter(in_list)
- return c.most_common(number)
- def howMany(in_list, name):
- c = Counter(in_list)
- return c[name]
- getIP() # This creates the IP list, has to always be run first.
- getCountries() # This creates another list that has all the countries from where the IPs came from.
- # It has a progress bar because it is going to take long, I didn't make it very efficient because
- # I don't know what kind of modules you're allowed to use.
- #print(getTop(countries,3)) # This gets the 3 countries more common on the list!
- #print(howMany(countries, "RU")) #This gets how many entries Russia has on the list
- #printList(ip_addr) # This prints the list of IP adresses.
Advertisement
Add Comment
Please, Sign In to add comment