C#-基于ip2region的離線IP定位庫
前言
在開發(fā)中,我們需要記錄關于登錄者的ip和位置信息,可以通過ip2region來實現(xiàn)!
比如說:
qq,gitee等 我們在異地登錄時,會提醒我們賬號異地登錄! 外賣等軟件,需要地理位置,如果我們的gps關閉,也可以根據(jù)IP分析地理位置等
簡介
ip2region - 準確率99.9%的離線IP地址定位庫,0.0x毫秒級查詢,ip2region.db數(shù)據(jù)庫只有數(shù)MB,提供了java,php,c,python,nodejs,golang,c#等查詢綁定和Binary,B樹,內(nèi)存三種查詢算法。每條ip數(shù)據(jù)段都固定了格式:
城市Id|國家|區(qū)域|省份|城市|ISP_
github地址:https://github.com/lionsoul2014/ip2region
使用
nuget安裝
Install-Package IP2Region
下載ip2region.db
git clone https://github.com/lionsoul2014/ip2region.git
然后找到data/文件夾下面找到ip2region.db復制到項目resources下
使用就很簡單了,代碼就一行
static void Main(string[] args)
{
using (var _search = new DbSearcher(Environment.CurrentDirectory + @"\DB\ip2region.db"))
{
Console.WriteLine(_search.MemorySearch("183.129.193.166").Region);
Console.Read();
}
}
總結
文章來源于即興發(fā)揮,雖然我工作中現(xiàn)在還沒遇到這種需求,但是了解一下總歸是有好處的,歡迎大家留言討論!
評論
圖片
表情
