-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip_geolocation.py
65 lines (56 loc) · 2.06 KB
/
ip_geolocation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import requests
import json
def print_banner():
banner = """
===========================================
IP Geolocation Tracker
Created By : Python 2.7
===========================================
"""
print banner
def get_geolocation(ip):
try:
url = "https://ipinfo.io/{}/json".format(ip)
response = requests.get(url)
data = response.json()
location = data.get('loc', 'Location not found')
city = data.get('city', 'City not found')
country = data.get('country', 'Country not found')
region = data.get('region', 'Region not found')
if location != 'Location not found':
lat, lon = location.split(',')
lat, lon = float(lat), float(lon)
else:
lat, lon = None, None
return {
'IP': ip,
'City': city,
'Region': region,
'Country': country,
'Latitude': lat,
'Longitude': lon
}
except requests.exceptions.RequestException as e:
return "Error in fetching data: {}".format(e)
def display_location(ip):
print "Searching for location of IP: {}".format(ip)
result = get_geolocation(ip)
if isinstance(result, dict):
print "\nLocation Information:"
print "IP Address: {}".format(result['IP'])
print "City: {}".format(result['City'])
print "Region: {}".format(result['Region'])
print "Country: {}".format(result['Country'])
if result['Latitude'] and result['Longitude']:
print "Latitude: {}, Longitude: {}".format(result['Latitude'], result['Longitude'])
else:
print "Location coordinates not available."
else:
print result
if __name__ == "__main__":
print_banner()
ip_input = raw_input("Enter IP address to track: ").strip()
if not ip_input:
ip_input = requests.get('https://api.ipify.org').text
print "Your IP is {}".format(ip_input)
display_location(ip_input)