用于IPv4到地理位置转换的小型Web服务器
ip2loc-server的Python项目详细描述
IP2Loc
用于IPv4到地理位置转换的小型Web服务器
系统先决条件
- SQLite可用 (默认情况下,these systems中包含sqlite。)
- 仅支持python3,首选
>= 3.6
。
快速启动
安装和运行
pip install --user ip2loc-server ip2loc
现在您已经启动了服务器。 尝试
curl localhost:8080/ip2loc?ip=216.58.221.228 # {"ip": "216.58.221.228", "country_code": "NL", "country_name": "Netherlands", "region_name": "Noord-Holland", "city_name": "Amsterdam", "latitude": 52.37403, "longitude": 4.88969} curl localhost:8080/url2loc?url=www.google.com # {"ip": "216.58.221.228", "country_code": "NL", "country_name": "Netherlands", "region_name": "Noord-Holland", "city_name": "Amsterdam", "latitude": 52.37403, "longitude": 4.88969} curl localhost:8080/url2loc?url=https://www.google.com # {"ip": "216.58.221.228", "country_code": "NL", "country_name": "Netherlands", "region_name": "Noord-Holland", "city_name": "Amsterdam", "latitude": 52.37403, "longitude": 4.88969}
测试服务器是否正常工作。运行ip2loc -h
获取更多帮助。
服务器端口
默认情况下,服务器侦听端口8080
,您只能在配置文件中修改此端口。
(不支持指定的侦听端口参数)
此外,多个端口可以配置为
# multiple listening port examplePORTS=(8080,8081,8082,)
路径
我故意不在此自述文件中列出数据、配置和日志路径, 因为默认路径都是相对路径,不容易描述清楚。
要查找这些有用的路径信息,请运行
ip2loc --showpath
跟踪最新数据
此项目中使用的所有数据都来自 https://lite.ip2location.com/database/ip-country-region-city-latitude-longitude。
由于此网站上的数据每月更新一次,您需要手动跟踪最新数据。
下载“ipv4csv”并记住当前版本的数据
运行
ip2loc --loaddata --dataver="current version" --csv="CSV/DATA/PATH/NAME.CSV"
或ip2loc --loaddata --dataver="current version" --zip="ZIP/DATA/PATH/NAME.ZIP"
工作原理
简而言之:有序IP数据的二进制搜索
见https://lite.ip2location.com/database/ip-country-region-city-latitude-longitude 有关数据结构的详细信息。