lua-users home
lua-l archive

[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, which links to:

The data file itself is at:

and is, IIRC, licensed under the GPL. The Lua module to query it is at:

It's licensed under a "What You Will" licence.

You use it something like this:

> require 'webnet77'
> w ='IpToCountry.csv')
> = w._VERSION
1.0.2 IP to country using the free database
> iso,name =  w:lookup('')
> = iso, name
> = w:lookup(2^24)
> = w:lookup(100663297)
> = w:lookup(2^32)
nil     bad ip number
> = w:lookup('')  --
> = w:lookup('')  -- MX
> = w:lookup('')
nil     bad ip string
> w = nil

Under Linux on a 1.8GHz Intel-chip laptop, load()ing takes about one
second (with the 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.)