Version 3 of IP-geolocation

Updated 2010-10-04 10:59:03 by fr

Example of getting geolocation information of IP's via free ipinfodb service

The code below is run from the command line with the ip as argument.

#!/bin/tclsh

package require http
package require JSONRPC

set ip $argv
puts "get geo data from $argv"

#get the desired data
set ht [::http::geturl http://ipinfodb.com/ip_query.php?output=json&timezone=false&ip=$ip]
set geo_ip [json::json2dict [::http::data $ht]]
::http::cleanup $ht

# some sample processing stuff
if {[dict get $geo_ip Status] == "OK"} {
    foreach key [dict keys $geo_ip] {
        set value [dict get $geo_ip $key]
        if {$value != ""} {
            puts "$key => $value"
        }
    }
}

Sample session:

 rob@rob-desktop:~/Public$ ./ip-geo 70.85.16.128
 get geo data from 70.85.16.128
 Ip => 70.85.16.128
 Status => OK
 CountryCode => US
 CountryName => United States
 RegionCode => 48
 RegionName => Texas
 City => Houston
 ZipPostalCode => 77002
 Latitude => 29.7523
 Longitude => -95.367