从各种地理位置数据库中获取给定IP地址的地理位置信息的简单工具。
ip2geotools的Python项目详细描述
说明
ip2geotools是一个简单的工具,用于从各种地理位置数据库中获取有关给定IP地址的地理位置信息。这个包为几个地理位置数据库提供了一个api。
安装
要安装ip2geotools模块,请键入:
$ pip install ip2geotools
基本用法
>>> fromip2geotools.databases.noncommercialimportDbIpCity>>> response=DbIpCity.get('147.229.2.90',api_key='free')>>> response.ip_address'147.229.2.90' >>> response.city'Brno (Brno střed)' >>> response.region'South Moravian' >>> response.country'CZ' >>> response.latitude49.1926824 >>> response.longitude16.6182105 >>> response.to_json()'{"ip_address": "147.229.2.90", "city": "Brno (Brno střed)", "region": "South Moravian", "country": "CZ", "latitude": 49.1926824, "longitude": 16.6182105}' >>> response.to_xml()'<?xml version="1.0" encoding="UTF-8" ?><ip_location><ip_address>147.229.2.90</ip_address><city>Brno (Brno střed)</city><region>South Moravian</region><country>CZ</country><latitude>49.1926824</latitude><longitude>16.6182105</longitude></ip_location>' >>> response.to_csv(',')'147.229.2.90,Brno (Brno střed),South Moravian,CZ,49.1926824,16.6182105'
命令行用法
安装后,您可以从命令行调用ip2geotools:
ip2geotools [-h] -d {dbipcity,hostip,freegeoip,ipstack,maxmindgeolite2city,ip2location,dbipweb,maxmindgeoip2city,ip2locationweb,neustarweb,geobytescitydetails,skyhookcontextacceleratorip,ipinfo,eurek,ipdata}[--api_key API_KEY][--db_path DB_PATH][-u USERNAME][-p PASSWORD][-f {json,xml,csv-space,csv-tab,inline}][-v] IP_ADDRESS
其中:
- ip2geotools:是安装在环境中的脚本,在开发过程中可以使用python -m ip2geotools代替
- IP_ADDRESS:要检查的IP地址
- {TT7}$,^ {TT8}$:显示帮助消息,退出
- -d{dbipcity,hostip,...,ipdata}:要使用的地理位置数据库(不区分大小写)
- --api_key API_KEY:给定地理位置数据库的api密钥(如果需要)
- --db_path DB_PATH:地理位置数据库文件的路径(如果需要)
- -u USERNAME,--username USERNAME:访问给定地理位置数据库的用户名(如果需要)
- -p PASSWORD,--password PASSWORD:访问给定地理位置数据库的密码(如果需要)
- -f{json,xml,csv-space,csv-tab,inline},--format{json,xml,csv-space,csv-tab,inline}:输出数据格式 >“$”,显示程序的版本号并退出< > >
示例:
$ ip2geotools 147.229.2.90 -d dbipcity -f json {"ip_address": "147.229.2.90", "city": "Brno (Brno střed)", "region": "South Moravian", "country": "CZ", "latitude": 49.1926824, "longitude": 16.6182105}
型号
此模块包含地理位置数据库返回数据的模型 这些模型也被用来比较给定和提供的数据。
ip2geotools.models.IpLocation
用于存储给定IP地址位置的模型。
属性:
- ip_address:IP地址
- city:IP地址所在城市
- region:IP地址所在的区域
- country:IP地址所在的国家(两个字母的国家代码)
- latitude:IP地址所在的纬度
- longitude:IP地址所在的经度
方法:
- to_json:返回json格式的模型数据
- to_xml:以XML格式返回模型数据(根元素:ip_location)
- to_csv:返回由给定分隔符分隔的csv格式的模型数据
- __str__:模型的内部字符串表示,新行上的每个信息
例外情况
此模块提供在访问数据时使用的特殊异常 第三方地理位置数据库。
- ip2geotools.errors.LocationError:一般位置错误
- ip2geotools.errors.IpAddressNotFoundError:找不到IP地址
- ip2geotools.errors.PermissionRequiredError:请求的身份验证或授权有问题;请检查访问服务的权限
- ip2geotools.errors.InvalidRequestError:无效请求
- ip2geotools.errors.InvalidResponseError:无效响应
- ip2geotools.errors.ServiceError:来自地理位置数据库的响应无效(不可访问等)
- ip2geotools.errors.LimitExceededError:已达到地理位置数据库的限制
数据库
以下类使用定义的接口访问许多不同的非商业和商业地理位置数据库。
ip2geotools.databases.interfaces
- IGeoIpDatabase:统一访问各种地理位置数据库提供的数据的接口
ip2geotools.databases.noncommercial
- DbIpCity:https://db-ip.com/api/
- HostIP:http://hostip.info/
- Freegeoip:http://freegeoip.net/数据库已弃用!
- Ipstack:https://ipstack.com/
- MaxMindGeoLite2City:https://dev.maxmind.com/geoip/geoip2/geolite2/
- Ip2Location:https://lite.ip2location.com/database/ip-country-region-city-latitude-longitude
ip2geotools.databases.commercial
- DbIpWeb:https://db-ip.com/
- MaxMindGeoIp2City:https://www.maxmind.com/
- Ip2LocationWeb:https://www.ip2location.com/
- ^{TT52}$:https://www.neustar.biz/resources/tools/ip-geolocation-lookup-tool/
- GeobytesCityDetails:http://geobytes.com/get-city-details-api/
- SkyhookContextAcceleratorIp:http://www.skyhookwireless.com/
- IpInfo:https://ipinfo.io/
- Eurek:https://www.eurekapi.com/
- Ipdata:https://ipdata.co/
要求
这段代码需要python 3.3+和requirements.txt中列出的其他几个包。
支架
请使用GitHub issue tracker报告此代码的所有问题
许可证
ip2geotools是在mit许可下发布的。有关详细信息,请参见捆绑的LICENSE文件。