Where is this IP address located?

Photo by Z on Unsplash
import socket
hostname = 'www.google.com'
def get_ip(hostname):
return socket.gethostbyname('google.com')
import requestsdef get_location(hostname):
ip = get_ip(hostname)
url = 'https://ipapi.co/{}/json/'.format(ip)
response = requests.get(url).json()
return {
"city": response.get("city"),
"country": response.get("country_name"),
"latitude": response.get("latitude"),
"longitude": response.get("longitude")
}
get_location('142.250.179.174'){
'city': 'Helsinki',
'country': 'Finland',
'latitude': 60.1719,
'longitude': 24.9347
}
{
'ip': '142.250.179.174',
'version': 'IPv4',
'city': 'Helsinki',
'region': 'Uusimaa',
'region_code': '18',
'country': 'FI',
'country_name': 'Finland',
'country_code': 'FI',
'country_code_iso3': 'FIN',
'country_capital': 'Helsinki',
'country_tld': '.fi',
'continent_code': 'EU',
'in_eu': True,
'postal': '00131',
'latitude': 60.1719,
'longitude': 24.9347,
'timezone': 'Europe/Helsinki',
'utc_offset': '+0200',
'country_calling_code': '+358',
'currency': 'EUR',
'currency_name': 'Euro',
'languages': 'fi-FI,sv-FI,smn',
'country_area': 337030.0,
'country_population': 5518050,
'asn': 'AS15169',
'org': 'GOOGLE'
}
import requestsAPI_KEY = <YOUR_APIKEY_HERE>def get_lat_lon(location):
loc = '{}, {}'.format(location['city'],
location['country'])
url = 'https://api.opencagedata.com/geocode/v1/json?' \
'q={}&key={}'.format(loc,
API_KEY)
result = requests.get(url).json()
if result['total_results'] > 0:
geo = result['results'][0]['geometry']
return {
"latitude": geo.get("lat"),
"longitude": geo.get("lng"),
}
else:
return None
location = get_location('www.google.com')geolocation = get_lat_lon(location)
print(geolocation)

{
'latitude': 60.1674881,
'longitude': 24.9427473
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store