I am looking for a way of analysing a massive list of IP addresses with location data. This list is about 10000000 entries.
For the moment I am using geoip2 module from maxmind. The original code can look for a single IP address entry and print results with a country code as shown below:
import geoip2.database
reader = geoip2.database.Reader('./GeoLite2-Country_20210330/GeoLite2-Country.mmdb')
response = reader.country('80.80.80.80')
print('response.country.iso_code: {}'.format(response.country.iso_code))
reader.close()
I trying to figure out a way of expanding this script to search for more entries at once by iterating through csv file. I wrote a code that does not work as I want to. It seems like Python does not like the way I am passing a list.
import geoip2.database
from csv import reader
import csv
read_db = geoip2.database.Reader('./GeoLite2-Country_20210330/GeoLite2-Country.mmdb')
with open('SrcIP.csv', 'r') as file1:
csv_read = csv.reader(file1, delimiter=' ', quotechar='|')
for row in csv_read:
response = read_db.country(', '.join(row))
print(response) #note 1
print('response.country: {}'.format(read_db.country)) #note 2
Note 1: If I print the response variable directly it searches the database for IP address range and prints all information available for each IP address. As a result, the output becomes too much for each line.
Note 2: This line will limit the output to only country code. However, it returns with error for each row as shown below:
response.country: <bound method Reader.country of <geoip2.database.Reader object at 0x7fe26a752c40>>