IP转Country和City

目录

需求描述

变更说明

各语言使用文档

GeoLite2数据库使用

第一种(推荐):下载数据库文件到本地,再定时两周更新一次数据库文件

第二种:通过web service访问GeoLite2数据库


需求描述

需要根据ip得知该ip所在的国家和城市。

变更说明

2019年12月30号MaxMind公司发布声明,使用免费的GeoLite2数据库的方式发生改变。
变化为
  1. Sign up for a MaxMind account (no purchase required)
  2. Set your password and create a license key
  3. 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 with license_key=YOUR_LICENSE_KEY. You will need to replace the YOUR_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 or curl 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)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值