Created
February 21, 2014 07:58
-
-
Save timofurrer/9130409 to your computer and use it in GitHub Desktop.
Python get IP Address information
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from sys import argv, stderr, exit | |
from geoip import geolite2 | |
IP_PROVIDER = "http://httpbin.org/ip" | |
comma_sep = lambda x: ", ".join([str(y) for y in x]) | |
if not argv[1:]: | |
from urllib2 import urlopen | |
from json import load | |
ip = load(urlopen(IP_PROVIDER))["origin"] | |
else: | |
ip = argv[1] | |
try: | |
match = geolite2.lookup(ip) | |
except ValueError, e: | |
stderr.write(str(e) + "\n") | |
exit(1) | |
if not match: | |
stderr.write("No IP %s found\n" % ip) | |
exit(2) | |
print("IP: %s" % match.ip) | |
print("Country: %s" % match.country) | |
print("Continent: %s" % match.continent) | |
print("Subvisions: %s" % comma_sep(match.subdivisions)) | |
print("Timezone: %s" % match.timezone) | |
print("Location: %s" % comma_sep(match.location)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python-geoip==1.2 | |
python-geoip-geolite2==2014.0207 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment