Last active
December 9, 2018 11:31
-
-
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 file contains 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
""" | |
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