Skip to content

Instantly share code, notes, and snippets.

@shojibMahabub
Last active December 9, 2018 11:31
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 shojibMahabub/8cfe7522b25a9a7686b92b08e7f359f7 to your computer and use it in GitHub Desktop.
Save shojibMahabub/8cfe7522b25a9a7686b92b08e7f359f7 to your computer and use it in GitHub Desktop.
A piece of code that can locate a given IP using ip-api(http://ip-api.com). Run it using 'python iplocator.py' command.
"""
This programme is responsible for finding
geo-location of an IP address using ip-api(http://ip-api.com)
Author : Mahabub Elahi
Version : 2.0
"""
import ipaddress
from requests import get
# taking input ip address from user
input_ip_address = input('Please enter the IP\n')
# get geo-location data using API
def find_location(ip):
"""
Get geo location data for the given ip address
"""
try:
ipaddress.ip_address(ip)
url = 'http://ip-api.com/json/{}'.format(ip)
response = get(url).json()
# formating according to requirements
print('{}, {}, ({})'.format(response['city'],
response['region'],
response['country']))
except:
print('Please enter a valid IP address')
return False
# calling find location def
find_location(input_ip_address)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment