Version 1 of IP-geolocation

Updated 2010-10-03 20:26:58 by RJM

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

Enter page contents here, upload content using the button above, or click cancel to leave it empty.