-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
34 lines (27 loc) · 1.01 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import sys
import geoip2.database
import collections
def handle_ips(file_name):
'''
Reads in each IP, lookup for country, print out iso_code and country name
Useful for later processing in Excel etc
Print out all countries ordered by most_common
:param file_name:
:return:
'''
country_count = collections.Counter()
with geoip2.database.Reader('GeoLite2-Country.mmdb') as reader:
with open(file_name, "r") as f:
for line_number, line in enumerate(f):
line = line.rstrip()
response = reader.country(line)
print(f"{line_number},{line},{response.country.iso_code},{response.country.name}")
country_count[response.country.name] += 1
print("-" * 10)
for country, count in country_count.most_common():
print(f"{country}:{count}")
if __name__ == '__main__':
if len(sys.argv) < 1:
print("please supply a file with the ip addresses")
sys.exit(1)
handle_ips(sys.argv[1])