Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5359355
  • 博文数量: 1144
  • 博客积分: 11974
  • 博客等级: 上将
  • 技术积分: 12312
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-13 20:06
文章存档

2017年(2)

2016年(14)

2015年(10)

2014年(28)

2013年(23)

2012年(29)

2011年(53)

2010年(86)

2009年(83)

2008年(43)

2007年(153)

2006年(575)

2005年(45)

分类: LINUX

2009-12-09 13:17:22

GeoIP Perl API

Requirements
For Geo::IP, must be installed. Geo::IP::PurePerl is also available, which doesn't require the C Library and contains no XS code.

Download
(faster, but requires C library)
(slower, but does not require C library)

Note: there have been reports of the Geo::IP 1.25 not working on FreeBSD, please try using Geo::IP 1.24 instead on FreeBSD.

Installing on Linux
This should work on most UNIX and GNU/Linux platforms.

perl Makefile.PL
make
make test
make install
More documentation is available from CPAN.

Installing on Windows
There is a PPM module available from Randy Kobes' PPM repository.

For ActivePerl 6xx builds, use:

C:\> ppm install 
which can be accessed within the ppm shell by setting the repository to:
and then using:
ppm> install Geo-IP
For 8xx builds, use:
C:\> ppm install 
which can be accessed within the ppm shell by setting the repository to:
  58
and then using:
ppm> install Geo-IP
In both cases, a post-install script should be run. The script will offer to fetch and install the GeoIP.dat database file; this is expected to go into the $ENV{programfiles}\GeoIP directory. If you don't run the post-install script, you can grab the database file from or .

Note there have been bug reports with using the GeoIP Perl API PPM. If you run into issues running the PPM on Windows, you may want to try the Geo::IP::PurePerl Module, which should run more reliably on Windows [].

Usage
Gets country name by hostname:

use Geo::IP;
my $gi = Geo::IP->new(GEOIP_STANDARD);
print $gi->country_name_by_name("amazon.com");
Gets US State by IP:
my $gi = Geo::IP->open("/usr/local/share/GeoIP/GeoIPRegion.dat", GEOIP_STANDARD);
my ($country, $region) = $gi->region_by_name("24.24.24.24");
Gets city by IP:
my $gi = Geo::IP->open("/usr/local/share/GeoIP/GeoIPCity.dat", GEOIP_STANDARD);
my $r = $gi->record_by_name("24.24.24.24");
print $record->country_code,
        $record->country_code3,
        $record->country_name,
        $record->region,
        $record->region_name,
        $record->city,
        $record->postal_code,
        $record->latitude,
        $record->longitude;
For additional examples, see t/ directory inside downloaded Perl API.

阅读(1977) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~