The process of getting IP address information is done based on GeoIP2 Databases. I Used the MaxMind GeoIP2 Python API for IP information queries. The Github page for the API is GeoIP2-python.
You need to download GeoLite2-City.mmdb as data source and install geoip2 module before you can use it.
For complete information, see MaxMind - GeoIP2 Downloadable Databases.
Installation
!pip install geoip2
# For Chinese user, you can choose tsinghua mirrors
#!pip install -i https://pypi.tuna.tsinghua.edu.cn/simple geoip2
Usage
First, we should import the geoip2 module. We are using the free downloaded data, therefore, please import geoip2.database .
import geoip2.database
ip = '129.59.93.0' # The IP of Vanderbilt Univeristy
reader = geoip2.database.Reader('GeoLite2-City.mmdb')
response = reader.city(ip)
reader.close()
all of the results were reserved in the response.
response
geoip2.models.City({'city': {'geoname_id': 4644585, 'names': {'de': 'Nashville', 'en': 'Nashville', 'es': 'Nashville', 'fr': 'Nashville', 'ja': 'ナッシュビル', 'pt-BR': 'Nashville', 'ru': 'Нашвилл', 'zh-CN': '纳什维尔'}}, 'continent': {'code': 'NA', 'geoname_id': 6255149, 'names': {'de': 'Nordamerika', 'en': 'North America', 'es': 'Norteamérica', 'fr': 'Amérique du Nord', 'ja': '北アメリカ', 'pt-BR': 'América do Norte', 'ru': 'Северная Америка', 'zh-CN': '北美洲'}}, 'country': {'geoname_id': 6252001, 'iso_code': 'US', 'names': {'de': 'USA', 'en': 'United States', 'es': 'Estados Unidos', 'fr': 'États-Unis', 'ja': 'アメリカ合衆国', 'pt-BR': 'Estados Unidos', 'ru': 'США', 'zh-CN': '美国'}}, 'location': {'accuracy_radius': 20, 'latitude': 36.066, 'longitude': -86.9659, 'metro_code': 659, 'time_zone': 'America/Chicago'}, 'postal': {'code': '37221'}, 'registered_country': {'geoname_id': 6252001, 'iso_code': 'US', 'names': {'de': 'USA', 'en': 'United States', 'es': 'Estados Unidos', 'fr': 'États-Unis', 'ja': 'アメリカ合衆国', 'pt-BR': 'Estados Unidos', 'ru': 'США', 'zh-CN': '美国'}}, 'subdivisions': [{'geoname_id': 4662168, 'iso_code': 'TN', 'names': {'en': 'Tennessee', 'es': 'Tennessee', 'fr': 'Tennessee', 'ja': 'テネシー州', 'pt-BR': 'Tenessi', 'ru': 'Теннесси', 'zh-CN': '田纳西州'}}], 'traits': {'ip_address': '129.59.93.0', 'prefix_len': 20}}, ['en'])
Let me re-format it to make it understandable.
{'city':
{'geoname_id': 4644585,
'names': {'de': 'Nashville', 'en': 'Nashville', 'es': 'Nashville', 'fr': 'Nashville', 'ja': 'ナッシュビル', 'pt-BR': 'Nashville', 'ru': 'Нашвилл', 'zh-CN': '纳什维尔'}
},
'continent':
{'code': 'NA',
'geoname_id': 6255149,
'names': {'de': 'Nordamerika', 'en': 'North America', 'es': 'Norteamérica', 'fr': 'Amérique du Nord', 'ja': '北アメリカ', 'pt-BR': 'América do Norte', 'ru': 'Северная Америка', 'zh-CN': '北美洲'}
},
'country':
{'geoname_id': 6252001,
'iso_code': 'US',
'names': {'de': 'USA', 'en': 'United States', 'es': 'Estados Unidos', 'fr': 'États-Unis', 'ja': 'アメリカ合衆国', 'pt-BR': 'Estados Unidos', 'ru': 'США', 'zh-CN': '美国'}
},
'location':
{'accuracy_radius': 20,
'latitude': 36.066,
'longitude': -86.9659,
'metro_code': 659,
'time_zone': 'America/Chicago'
},
'postal':
{'code': '37221'},
'registered_country':
{'geoname_id': 6252001,
'iso_code': 'US',
'names': {'de': 'USA', 'en': 'United States', 'es': 'Estados Unidos', 'fr': 'États-Unis', 'ja': 'アメリカ合衆国', 'pt-BR': 'Estados Unidos', 'ru': 'США', 'zh-CN': '美国'}
},
'subdivisions':
[{'geoname_id': 4662168,
'iso_code': 'TN',
'names': {'en': 'Tennessee', 'es': 'Tennessee', 'fr': 'Tennessee', 'ja': 'テネシー州', 'pt-BR': 'Tenessi', 'ru': 'Теннесси', 'zh-CN': '田纳西州'}
}],
'traits': {'ip_address': '129.59.93.0', 'prefix_len': 20}
},
['en']
There are some example to use the response:
response.country.iso_code
#'US'
response.country.name
#'United States'
response.country.names['zh-CN']
#'美国'
response.subdivisions.most_specific.name
#'Tennessee'
response.subdivisions.most_specific.iso_code
#'TN'
response.city.name
#'Nashville'
response.postal.code
#'37221'
response.location.latitude
#36.066
response.location.longitude
#-86.9659
response.traits.network
#IPv4Network('129.59.80.0/20')
With this method, you can convert IP address information into actual address or latitude and longitude information, etc., for exploratory data analysis or modeling.