lua-users home
lua-l archive

ANN: IP-to-country lookup in Lua

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


A company called Webnet77 publishes a free IP-to-Country database which, 
though less detailed than other free geolocation data such as that from 
Maxmind, consists of a single CSV file that's currently under 6MB in size 
(under 1MB to download as a .gz file). It's updated every 24 hours.
This seemed a good sort of "small is beautiful" match for Lua, so I have 
written a Lua module, consisting of a single Lua file, to use this data 
for IP-to-country lookup.
Information about this stuff is provided under the Tools menu of the site 
http://webnet77.com/, which links to:
http://software77.net/cgi-bin/ip-country/geo-ip.pl
The data file itself is at:
http://software77.net/cgi-bin/ip-country/geo-ip.pl?action=download
and is, IIRC, licensed under the GPL. The Lua module to query it is at:
http://software77.net/geo/webnet77.lua.gz
It's licensed under a "What You Will" licence.
You use it something like this:
> require 'webnet77'
> w = webnet77.new('IpToCountry.csv')
> = w._VERSION
1.0.2 IP to country using the free webnet77.com database
> iso,name = w:lookup('127.0.0.1')
> = iso, name
ZZ RESERVED
> = w:lookup(2^24)
-- UNALLOCATED
> = w:lookup(100663297)
US UNITED STATES
> = w:lookup(2^32)
nil bad ip number
> = w:lookup('87.237.62.181') -- www.lua.org
UK UNITED KINGDOM
> = w:lookup('138.82.85.1') -- tecgraf.puc-rio.br MX
BR BRAZIL
> = w:lookup('76.256.8.1')
nil bad ip string
> w = nil
Under Linux on a 1.8GHz Intel-chip laptop, load()ing takes about one
second (with the webnet77.com data at 6MB uncompressed). Throughput
is about 50,000 lookup()s per second for IP strings and 60,000 per
second when you pass in u32s (since Lua doesn't need to parse the
strings and multiply out the raw IP number.)

AltStyle によって変換されたページ (->オリジナル) /