Skip to content

Instantly share code, notes, and snippets.

@tzermias
Created January 7, 2014 21: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 tzermias/8307363 to your computer and use it in GitHub Desktop.
Save tzermias/8307363 to your computer and use it in GitHub Desktop.
Script that queries the GEOIP2 Precision Demo by Maxmind from the commandline. Not fully tested.
#! /usr/bin/env python
import urllib2
import json
import sys
def main(args):
if len(args) != 2:
print "%s IPADDR" % args[0]
exit(255)
url = "https://www.maxmind.com/geoip/v2.0/city_isp_org/%s?demo=1"
f = urllib2.urlopen(url % args[1])
response = json.loads(f.read())
f.close()
print response
print "IP Address: %s" % response['traits']['ip_address']
print "Country code: %s" % response['country']['iso_code']
print "Location: %s" % ", ".join(
[response[r]['names']['en'] for r in ['city'] if
response.has_key(r)] +
[response['subdivisions'][r]['names']['en'] for
r in range(len(response['subdivisions']))] if
response.has_key('subdivisions') else [] +
[response[r]['names']['en'] for r in ['country','continent'] if
response.has_key(r)])
if response.has_key('postal'):
print "Postal code: %s" % response['postal']['code']
print "Coordinates: %s, %s" % (response['location']['latitude'],
response['location']['longitude'])
if response['traits'].has_key('isp'):
print "ISP: %s" % response['traits']['isp']
if response['traits'].has_key('organization'):
print "Organization: %s" % response['traits']['organization']
if response['traits'].has_key('autonomous_system_number'):
print "Autonomous System Number: %s" %\
response['traits']['autonomous_system_number']
if response['traits'].has_key('domain'):
print "Domain: %s" % response['traits']['domain']
if response['location'].has_key('metro_code'):
print "Metro Code: %s" % response['location']['metro_code']
print "Remaining queries: %s" % response['maxmind']['queries_remaining']
if __name__ == '__main__':
main(sys.argv)
# vi: ts=4 sts=4 et ai sw=4 tw=80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment