-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocation_details.py
34 lines (26 loc) · 971 Bytes
/
location_details.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
import requests
def get_location_details():
try:
response = requests.get('https://ipinfo.io/')
data = response.json()
location_details = {
'IP': data.get('ip', 'Not Available'),
'City': data.get('city', 'Not Available'),
'Region': data.get('region', 'Not Available'),
'Country': data.get('country', 'Not Available'),
'Coordinates': data.get('loc', 'Not Available') # Latitude and Longitude
}
return location_details
except Exception as e:
print(f"An error occurred: {e}")
return None
def location_details_string(location):
out = ""
if location:
for key, value in location.items():
out += f"{key}: {value}\n"
return out
# This block will only execute if the script is run directly (not imported)
if __name__ == "__main__":
location = get_location_details()
location_details_string(location)