어느나라 ip인지 알아내는 방법.
분류없음 2008/11/25 13:28 ip address <-> ip number 전환코드를 찾다가 찾아낸 조금 재미있는 것. ip대역 CSV 파일(GeoIp Country CSV)을 구해야함.
심심하면 함 만들어 보심.
GeoIP Country CSV Text Files
MaxMind GeoIP databases are available in a Comma Separated Value (CSV) format, in addition to the binary format. These CSV files generally contain IP Address range and geographical data for all publicly assigned IPv4 addresses.
Due to the large size of geolocation databases, we generally recommend using our binary format with one of our APIs, since they are highly optimized for speed and disk space. On the other hand, if you have a requirement to import the data into a SQL database, the CSV format is recommended. We have listed some guidelines for importing and querying the data with a SQL database.
CSV Format
The CSV File contains four fields:
* Beginning IP Number*
* Ending IP Number*
* ISO 3166 Country Code
* Country Name
This is an sample of how the CSV file is structured:
"begin_num","end_num","country","name"
"1029177344","1029439487","AU","Australia"
"1029439488","1029570559","HK","Hong Kong"
"1029570560","1029572607","ID","Indonesia"
* Beginning IP Number and Ending IP Number are calculated as follows:
ipnum = 16777216*w + 65536*x + 256*y + z (1)
where
IP Address = w.x.y.z
The reverse of this formula is
w = int ( ipnum / 16777216 ) % 256;
x = int ( ipnum / 65536 ) % 256;
y = int ( ipnum / 256 ) % 256;
z = int ( ipnum ) % 256;
Where % is the mod operator.
Here is sample Perl code to convert the IP number to
a IP address:
sub numToStr {
my ($ipnum) = @_;
my $z = $ipnum % 256;
$ipnum >>= 8;
my $y = $ipnum % 256;
$ipnum >>= 8;
my $x = $ipnum % 256;
$ipnum >>= 8;
my $w = $ipnum % 256;
return "$w.$x.$y.$z";
}
It is useful to have the IP Number if you are performing IP Address lookups using a database. For example the following queries will find the country based on IP Address 24.24.24.24:
SQL Query
SELECT ip_country FROM geoip WHERE 404232216 BETWEEN begin_ip_num AND end_ip_num
MySQL Query
SELECT ip_country FROM geoip WHERE 404232216 >= begin_ip_num AND
404232216 <= end_ip_num
Here we used the formula (1) to compute the IP Number based on 24.24.24.24
404232216 = 16777216*24 + 65536*24 + 256*24 + 24
For more information on importing GeoIP CSV files into MySQL, see HOW-TO Import the MaxMind GeoIP Free Country CSV file into MySQL and save diskspace.
http://www.delau.net/php/geoip.html
심심하면 함 만들어 보심.
GeoIP Country CSV Text Files
MaxMind GeoIP databases are available in a Comma Separated Value (CSV) format, in addition to the binary format. These CSV files generally contain IP Address range and geographical data for all publicly assigned IPv4 addresses.
Due to the large size of geolocation databases, we generally recommend using our binary format with one of our APIs, since they are highly optimized for speed and disk space. On the other hand, if you have a requirement to import the data into a SQL database, the CSV format is recommended. We have listed some guidelines for importing and querying the data with a SQL database.
CSV Format
The CSV File contains four fields:
* Beginning IP Number*
* Ending IP Number*
* ISO 3166 Country Code
* Country Name
This is an sample of how the CSV file is structured:
"begin_num","end_num","country","name"
"1029177344","1029439487","AU","Australia"
"1029439488","1029570559","HK","Hong Kong"
"1029570560","1029572607","ID","Indonesia"
* Beginning IP Number and Ending IP Number are calculated as follows:
ipnum = 16777216*w + 65536*x + 256*y + z (1)
where
IP Address = w.x.y.z
The reverse of this formula is
w = int ( ipnum / 16777216 ) % 256;
x = int ( ipnum / 65536 ) % 256;
y = int ( ipnum / 256 ) % 256;
z = int ( ipnum ) % 256;
Where % is the mod operator.
Here is sample Perl code to convert the IP number to
a IP address:
sub numToStr {
my ($ipnum) = @_;
my $z = $ipnum % 256;
$ipnum >>= 8;
my $y = $ipnum % 256;
$ipnum >>= 8;
my $x = $ipnum % 256;
$ipnum >>= 8;
my $w = $ipnum % 256;
return "$w.$x.$y.$z";
}
It is useful to have the IP Number if you are performing IP Address lookups using a database. For example the following queries will find the country based on IP Address 24.24.24.24:
SQL Query
SELECT ip_country FROM geoip WHERE 404232216 BETWEEN begin_ip_num AND end_ip_num
MySQL Query
SELECT ip_country FROM geoip WHERE 404232216 >= begin_ip_num AND
404232216 <= end_ip_num
Here we used the formula (1) to compute the IP Number based on 24.24.24.24
404232216 = 16777216*24 + 65536*24 + 256*24 + 24
For more information on importing GeoIP CSV files into MySQL, see HOW-TO Import the MaxMind GeoIP Free Country CSV file into MySQL and save diskspace.
http://www.delau.net/php/geoip.html
Trackback Address :: http://www.microstrong.pe.kr/tt/trackback/25

