版权声明:本文为博主徐松亮的原创作品,未经允许不得转载,多谢支持!QQ:5387603
推荐点击此链接:欢迎进入徐松亮博客一站式导航搜索(随时更新)
目录
一,概念
场景
场景解决
具体方案
数据到哪去查呢?
本文用什么变成语言?
本文用GeoLiteCity的在线查询还是离线查询?
离线数据源的定期更新
二,准备工作
python环境
安装MaxMind公司的开源数据库GeoLiteCity:
三,代码讲解
四,源码
五,运行效果
六,调试记录
pip install geoip2
等待安装完成
下载数据包
下载网址:https://dev.maxmind.com/zh-hans/geoip/geoip2/geolite2/
#!D:/Program Files/Python37/python import geoip2.database # This creates a Reader object. You should use the same object # across multiple requests as creation of it is expensive. reader = geoip2.database.Reader( '../../../xsl_use_lib/geolite2/GeoLite2-City/GeoLite2-City.mmdb') def ip_print_AddrInfo(ip): # Replace "city" with pthe method corresponding to the database # that you are using, e.g., "country". #----------------------------------------------- # 载入指定IP相关数据 response = reader.city(ip) #读取国家代码 Country_IsoCode = response.country.iso_code #读取国家名称 Country_Name = response.country.name #读取国家名称(中文显示) Country_NameCN = response.country.names['zh-CN'] #读取州(国外)/省(国内)名称 Country_SpecificName = response.subdivisions.most_specific.name #读取州(国外)/省(国内)代码 Country_SpecificIsoCode = response.subdivisions.most_specific.iso_code #读取城市名称 City_Name = response.city.name #读取邮政编码 City_PostalCode = response.postal.code #获取纬度 Location_Latitude = response.location.latitude #获取经度 Location_Longitude = response.location.longitude #------------------------------------------------打印 print('[*] Target: ' + ip + ' GeoLite2-Located ') print(' [+] Country_IsoCode : ' + Country_IsoCode) print(' [+] Country_Name : ' + Country_Name) print(' [+] Country_NameCN : ' + Country_NameCN) print(' [+] Country_SpecificName : ' + Country_SpecificName) print(' [+] Country_SpecificIsoCode: ' + Country_SpecificIsoCode) print(' [+] City_Name : ' + City_Name) if City_PostalCode != None: print(' [+] City_PostalCode : ' + City_PostalCode) print(' [+] Location_Latitude : ' + str(Location_Latitude)) print(' [+] Location_Longitude : ' + str(Location_Longitude)) ip = '119.108.116.209' ip_print_AddrInfo(ip)