RachelW
RachelW

Reputation: 1

How to get the country code for multiple IP addresses at a time (5 or more) using geoip2 database?

This is how I've been getting the country name for one IP address at a time, but I need to be able to do multiple, sometimes over 50 at a time.

>>>import geoip2.database
>>>
>>>reader = geoip2.database.Reader('/path/to/GeoLite2-City.mmdb')
>>>
>>>response = reader.city('128.101.101.101')
>>>
>>>response.country.iso_code
>>>
>>>response.country.name

Upvotes: 0

Views: 259

Answers (2)

Marildo Rufino
Marildo Rufino

Reputation: 1

print(response.city.name) and print(response.traits.network)

Upvotes: 0

Sam Williams Jebaraj
Sam Williams Jebaraj

Reputation: 144

put all the ip's to a list and iterate through the list.

reader = geoip2.database.Reader('/path/to/GeoLite2-City.mmdb')
ip_list=['128.101.101.101','198.101.101.101','208.101.101.101','120.101.101.101','129.101.101.101','138.101.101.101','148.101.101.101']
for ip in ip_list:
    response = reader.city(ip)
    print response.country.iso_code
    print response.country.name

or add the ip's to an excel sheet and use pandas or xlrd to read the ip's to a list and iterate them again as shown above.

Upvotes: 0

Related Questions