Jonny Zheng

I'm an idealist, not perfectionist

根据IP获得城市名称 Ruby 版

很多本地化生活网站的一个基本功能就是根据用户的IP地址判断所在的城市,基本的逻辑就是有一个ip库,根据ip库里的记录找到用户ip地址所在的范围。

IP库没有太多选择,我在网上找到的一个http://www.maxmind.com/app/ip-location, 这个网站本身提供收费的IP库和免费的IP库,收费的库在准确率上要稍微高一些,拿城市的库做一个例子,左边是免费版,右边是收费版:

GeoLite City GeoIP City
Cost Free $370 initial, $90 per month updates
Coverage Worldwide Worldwide
Accuracy Over 99.5% on a country level and 79% on a city level for the US within a 25 mile radius. More details Over 99.8% on a country level and 83% on a city level for the US within a 25 mile radius. More details
Redistribution Free, subject to GPL/LGPL for APIs and database licenseCommercial redistribution licenses are available Please contact us.
Updates Updated monthly, at the beginning of each month Updated monthly. For binary format, weekly updates, automated updates available by using geoipupdate program included withC API

ruby 的使用很简单,有一个gem库: http://geoip.rubyforge.org/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
require ‘geoip’
c = GeoIP.new(‘GeoIP.dat’).country(‘www.nokia.com’)
=> [“www.nokia.com”, “147.243.3.83”, 69, “FI”, “FIN”, “Finland”, “EU”]
c.country_code3
=> “FIN”
c.to_hash
=> {:country_code3=>"FIN", :country_name=>"Finland", :continent_code=>"EU",
:request=>"www.nokia.com", :country_code=>69, :country_code2=>"FI", :ip=>"147.243.3.83"}


c = GeoIP.new(‘GeoLiteCity.dat’).city(‘github.com’)
=> [“github.com”, “207.97.227.239”, “US”, “USA”, “United States”, “NA”, “CA”,
“San Francisco”, “94110”, 37.7484, -122.4156, 807, 415, “America/Los_Angeles”]
>> c.longitude
=> -122.4156
>> c.timezone
=> “America/Los_Angeles”


c = GeoIP.new(‘GeoIPASNum.dat’).asn(“www.fsb.ru”)
=> [“AS8342”, “RTComm.RU Autonomous System”]

我下了一个citylite的免费库,只试了我自己的地址,判断是准确的,还没有找其他的地方进行测试,79%的准确率不高也不低,用作预判断还是可以接受的。

Comments