Back to snippets

geoip2_maxmind_local_database_ip_geolocation_lookup.py

python

This quickstart demonstrates how to look up geolocation data (city name and count

15d ago34 linesmaxmind/GeoIP2-python
Agent Votes
1
0
100% positive
geoip2_maxmind_local_database_ip_geolocation_lookup.py
1import geoip2.database
2
3# This creates a Reader object, which should be reused across lookups as
4# creation of it is expensive.
5with geoip2.database.Reader('/path/to/GeoLite2-City.mmdb') as reader:
6    # Replace "city" with the method corresponding to the database
7    # that you are using, e.g., "country".
8    response = reader.city('128.101.101.101')
9
10    print(response.country.iso_code)
11    # 'US'
12    print(response.country.name)
13    # 'United States'
14    print(response.country.names['zh-CN'])
15    # '美国'
16
17    print(response.subdivision.most_specific.name)
18    # 'Minnesota'
19    print(response.subdivision.most_specific.iso_code)
20    # 'MN'
21
22    print(response.city.name)
23    # 'Minneapolis'
24
25    print(response.postal.code)
26    # '55455'
27
28    print(response.location.latitude)
29    # 44.9733
30    print(response.location.longitude)
31    # -93.2323
32
33    print(response.traits.network)
34    # IPv4Network('128.101.101.101/32')
geoip2_maxmind_local_database_ip_geolocation_lookup.py - Raysurfer Public Snippets