問題描述
我已下載 ip-to-country.csv,其中包含映射到國家/地區的 IP 范圍.我應該如何將這些數據存儲到數據庫中,如何查詢 IP 地址在什么范圍內才能知道 IP 地址來自哪里?
I have downloaded ip-to-country.csv that has ip ranges that are mapped to countries. How should I store this data to database and how can I query in what range Ip address is to know where Ip address is coming from?
推薦答案
我寫了一個小庫,叫做 ip2c 做到這一點.它使用來自 webhosting.info 的數據庫,但也支持來自 Software77.
I wrote a small lib called ip2c to do just that. it uses the database from webhosting.info but also supports that from Software77.
它將 CSV 信息轉換為緊湊的二進制格式,并且可以直接在文件、內存或內存映射文件中進行搜索.
It converts the CSV info a compact binary format and can do the search straight on the file, in memory or in a memory mapped file.
Java API 用法類似:
The Java API usage is similar to this:
String ip = 85.64.225.159;
int caching1 = IP2Country.NO_CACHE; // Straight on file, Fastest startup, slowest queries
int caching2 = IP2Country.MEMORY_MAPPED; // Memory mapped file, fast startup, fast queries.
int caching3 = IP2Country.MEMORY_CACHE; // load file into memory, slowerst startup, fastest queries
IP2Country ip2c = new IP2Country(caching1);
Country c = ip2c.getCountry(ip);
if (c == null)
{
System.out.println("UNKNOWN");
}
else
{
// will output IL ISR ISRAEL
System.out.println(c.get2cStr() + " " + c.get3cStr() + " " + c.getName());
}
這篇關于帶有數據庫的國家/地區的 IP 地址的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!