目录
第一种(推荐):下载数据库文件到本地,再定时两周更新一次数据库文件
第二种:通过web service访问GeoLite2数据库
需求描述
需要根据ip得知该ip所在的国家和城市。
变更说明
2019年12月30号MaxMind公司发布声明,使用免费的GeoLite2数据库的方式发生改变。
变化为
- Sign up for a MaxMind account (no purchase required)
- Set your password and create a license key
- Setup your download mechanism by using our GeoIP Update program or creating a direct download script
使用MaxMind账户创建的lincense key如下
# Account/User ID :<Account ID>
# License key :<License Key>
参考链接:https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-geolite2-databases/
各语言使用文档
https://dev.maxmind.com/geoip/geoip2/downloadable/#MaxMind_Supported_APIs
GeoLite2数据库使用
第一种(推荐):下载数据库文件到本地,再定时两周更新一次数据库文件
因为免费数据库是两周更新一次
下载方式参考链接https://dev.maxmind.com/geoip/geoipupdate/#Direct_Downloads:下载链接需要做如下转换
In order to download the databases from a script or program, please use the “direct download URL” on our GeoIP download page and make the following changes:
- Replace
/geoip_download_by_token
with/geoip_download
- Replace
token=XXXX
withlicense_key=YOUR_LICENSE_KEY
. You will need to replace theYOUR_LICENSE_KEY
placeholder with an active license key associated with your MaxMind account.- If you wish to always download the latest database, remove the date parameter from the URL.
- If you are using
wget
orcurl
from a shell script, please be sure to quote the URL.
定时更新脚本
#!/bin/bash`
SCRIPT_PATH=$(cd "$(dirname "$0")";cd ../;pwd)
IP_DB=${SCRIPT_PATH}/conf
[ -d "${IP_DB}" ] || mkdir -p "${IP_DB}"
IPTOOLS_PATH=${IP_DB}/GeoIP
[ -d "${IPTOOLS_PATH}" ] || mkdir -p "${IPTOOLS_PATH}"
# 下载、解压、移动、删除
function download
{
wget -t 5 -O "${IPTOOLS_PATH}"/geolitecity.tar.gz "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=<License Key>&suffix=tar.gz"
tar -xzvf "${IPTOOLS_PATH}"/geolitecity.tar.gz -C "${IPTOOLS_PATH}"
mv -f "${IPTOOLS_PATH}"/Geo*/*.mmdb "${IP_DB}"/GeoLite2-City.mmdb
rm -rf "${IPTOOLS_PATH}"
}
echo "begin download...at $(date +%Y%m%d_%H%M%S)"
download
echo "download end! at $(date +%Y%m%d_%H%M%S)"
下载后解压,geolitecity目录中有GeoLite2-City.mmdb文件
Python代码示例
import geoip2.database
reader = geoip2.database.Reader('GeoLite2-City.mmdb')
data = {'remote_addr': '37.129.226.14'}
response = reader.city(data.get("remote_addr"))
# print(response)
if response.country.iso_code is None:
data["ip_country"] = 'na'
else:
data["ip_country"] = response.country.iso_code
if response.city.name is None:
data["ip_city"] = 'na'
data["ip_city"] = response.city.name
else:
pass
print(data)
第二种:通过web service访问GeoLite2数据库
(貌似需要付费,原本在本地测试失败,显示没有访问权限,然后申请了5$的试用额度,该测试可正常运行了,是否是申请了额度的原因待定)
Web Service代码示例
import geoip2.webservice
reader = geoip2.webservice.Client(<Account ID>, <License Key>)
data = {'remote_addr': '37.129.226.14'}
response = reader.city(data.get("remote_addr"))
# print(response)
if response.country.iso_code is None:
data["ip_country"] = 'na'
else:
data["ip_country"] = response.country.iso_code
if response.city.name is None:
data["ip_city"] = 'na'
data["ip_city"] = response.city.name
else:
pass
print(data)