IP地址到有数据库的国家 [英] Ip address to country with database

查看:179
本文介绍了IP地址到有数据库的国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经下载了ip-to-country.csv,它具有映射到国家/地区的ip范围。我应该如何将这些数据存储到数据库中,以及如何查询IP地址的范围以了解IP地址来自何处?

我写了一个名为 ip2c 的小库来做到这一点。它使用 webhosting.info 中的数据库,但也支持来自 Software77



它将CSV信息转换为紧凑的二进制格式,并可以直接在文件,内存或内存映射文件中进行搜索。

Java API用法与此类似:

 字符串ip = 85.64.225.159; 
int caching1 = IP2Country.NO_CACHE; //直接文件,最快启动,最慢查询
int caching2 = IP2Country.MEMORY_MAPPED; //内存映射文件,快速启动,快速查询。
int caching3 = IP2Country.MEMORY_CACHE; //将文件加载到内存中,启动速度较慢,查询速度最快
IP2Country ip2c = new IP2Country(caching1);
国家c = ip2c.getCountry(ip);
if(c == null)
{
System.out.println(UNKNOWN);
}
else
{
//将输出IL ISR ISRAEL
System.out.println(c.get2cStr()++ c.get3cStr() ++ c.getName());
}


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?

解决方案

I wrote a small lib called ip2c to do just that. it uses the database from webhosting.info but also supports that from Software77.

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.

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地址到有数据库的国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆