Skip to content

Instantly share code, notes, and snippets.

@404pnf
Forked from Sulter/geoip
Created November 14, 2013 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 404pnf/7459226 to your computer and use it in GitHub Desktop.
Save 404pnf/7459226 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import urllib2
import simplejson
def get_info(adress):
print "************************************************"
api = "http://freegeoip.net/json/" + adress
try:
result = simplejson.load(urllib2.urlopen(api))
except urllib2.HTTPError:
print "Could not find: %s" % adress
return None
print adress
print "IP: %s" % result["ip"]
print "Country Name: %s" % result["country_name"]
print "Country Code: %s" % result["country_code"]
print "Region Name: %s" % result["region_name"]
print "Region Code: %s" % result["region_code"]
print "City: %s" % result["city"]
print "Zip Code: %s" % result["zipcode"]
print "Latitude: %s" % result["latitude"]
print "Longitude: %s" % result["longitude"]
print "Location link: " + "http://www.openstreetmap.org/#map=11/" + str(result["latitude"]) +"/" + str(result["longitude"])
def showhelp():
print "Usage: geoip address [address]..."
print "find gelocation of IP addresses and host names, using http://freegeoip.net/"
if __name__ == "__main__": #code to execute if called from command-line
inputs = sys.argv
if len(inputs) < 2 or "--help" in inputs:
showhelp()
else:
for address in inputs[1:]:
get_info(address)
print "************************************************"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment