Created
January 22, 2025 02:44
-
-
Save smaldd14/5ff432ec372a0c620e61680635ba8ae8 to your computer and use it in GitHub Desktop.
Gather domain location info
This file contains hidden or 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
import socket | |
import requests | |
import json | |
from urllib.parse import urlparse | |
def get_website_location(url): | |
""" | |
Determine the hosting location of a website using IP geolocation. | |
Args: | |
url (str): The website URL (e.g., 'www.example.com' or 'https://example.com') | |
Returns: | |
dict: Location information including country, region, and city | |
""" | |
# Clean and parse the URL | |
if not url.startswith(('http://', 'https://')): | |
url = 'https://' + url | |
domain = urlparse(url).netloc | |
try: | |
# Get IP address | |
ip_address = socket.gethostbyname(domain) | |
# Use ip-api.com for geolocation (free, no API key required) | |
response = requests.get(f'http://ip-api.com/json/{ip_address}') | |
data = response.json() | |
if data['status'] == 'success': | |
return { | |
'ip': ip_address, | |
'country': data['country'], | |
'country_code': data['countryCode'], | |
'region': data['regionName'], | |
'city': data['city'], | |
'isp': data['isp'] | |
} | |
else: | |
return {'error': 'Could not determine location'} | |
except socket.gaierror: | |
return {'error': 'Could not resolve domain'} | |
except requests.RequestException: | |
return {'error': 'Could not fetch location data'} | |
except KeyError: | |
return {'error': 'Incomplete location data'} | |
def main(): | |
website = input("Enter website URL: ") | |
result = get_website_location(website) | |
if 'error' not in result: | |
print("\nWebsite Hosting Information:") | |
print(f"IP Address: {result['ip']}") | |
print(f"Country: {result['country']} ({result['country_code']})") | |
print(f"Region: {result['region']}") | |
print(f"City: {result['city']}") | |
print(f"ISP: {result['isp']}") | |
else: | |
print(f"\nError: {result['error']}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment