Skip to content

Instantly share code, notes, and snippets.

@ammarx
Created April 20, 2017 10:39
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 ammarx/d4155116fe141686c0bca77d8c0aebb8 to your computer and use it in GitHub Desktop.
Save ammarx/d4155116fe141686c0bca77d8c0aebb8 to your computer and use it in GitHub Desktop.
get_ip_info.py
import urllib, json, sys
# output here
try:
url = "http://ip-api.com/json/" + str(sys.argv[1])
response = urllib.urlopen(url)
data = json.loads(response.read())
print "Argument: " + str(sys.argv[1])
try:
print "City: " + data["city"]
except:
print "City: N/A"
try:
print "Country: " + data["country"]
except:
print "Country: N/A"
try:
print "Country Code: " + data["countryCode"]
except:
print "Country Code: N/A"
try:
print "ISP: " + data["isp"]
except:
print "ISP: N/A"
try:
print "Lat: " + str(data["lat"])
except:
print "Lat: N/A"
try:
print "Lon: " + str(data["lon"])
except:
print "Lon: N/A"
try:
print "Query: " + data["query"]
except:
print "Query: N/A"
except Exception:
print "Connection error or invalid argument!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment