GeoIP2 对象是对 MaxMind geoip2 Python 库 的包装。[1]
In order to perform IP-based geolocation, the GeoIP2 object requires
the geoip2 Python package and the GeoIP Country and/or City
datasets in binary format (the CSV files will not work!), downloaded from e.g.
MaxMind or DB-IP websites. Grab the GeoLite2-Country.mmdb.gz and
GeoLite2-City.mmdb.gz files and unzip them in a directory corresponding to
the GEOIP_PATH setting.
另外,建议安装 libmaxminddb C 库 ,这样 geoip2 就可以利用 C 库更快的速度。
Support for .mmdb files downloaded from DB-IP was added.
Here is an example of its usage:
>>> from django.contrib.gis.geoip2 import GeoIP2
>>> g = GeoIP2()
>>> g.country("google.com")
{'country_code': 'US', 'country_name': 'United States'}
>>> g.city("72.14.207.99")
{'city': 'Mountain View',
'continent_code': 'NA',
'continent_name': 'North America',
'country_code': 'US',
'country_name': 'United States',
'dma_code': 807,
'is_in_european_union': False,
'latitude': 37.419200897216797,
'longitude': -122.05740356445312,
'postal_code': '94043',
'region': 'CA',
'time_zone': 'America/Los_Angeles'}
>>> g.lat_lon("salon.com")
(39.0437, -77.4875)
>>> g.lon_lat("uh.edu")
(-95.4342, 29.834)
>>> g.geos("24.124.1.80").wkt
'POINT (-97 38)'
GeoIP2(path=None, cache=0, country=None, city=None)¶GeoIP 对象不需要任何参数来使用默认设置。然而,至少 GEOIP_PATH 配置应该用你 GeoIP 数据集位置的路径来设置。以下初始化关键字可用于自定义任何默认配置。
| 关键字参数 | 描述 | 
|---|---|
path | 
GeoIP 数据所在的基本目录或城市或国家数据文件(.mmdb)所在的完整路径。假设城市和国家数据集都在这个目录中;覆盖 GEOIP_PATH 配置。 | 
cache | 
打开 GeoIP 数据集时的缓存配置。可以是(0、1、2、4、8)的整数,分别对应于 MODE_AUTO、MODE_MMAP_EXT、MODE_MMAP、GEOIP_INDEX_CACHE 和 MODE_MEMORY C API 配置。默认值为 0(MODE_AUTO)。 | 
country | 
GeoIP 国家数据文件的名称。默认值为 GeoLite2-Country.mmdb。设置这个关键字会覆盖 GEOIP_COUNTRY 的配置。 | 
city | 
GeoIP 城市数据文件的名称。默认为 GeoLite2-City.mmdb。设置这个关键字会覆盖 GEOIP_CITY 的配置。 | 
12月 05, 2023