|
import streamlit as st |
|
import socket |
|
import requests |
|
import pycountry |
|
from urllib.parse import urlparse |
|
|
|
|
|
def get_domain(url): |
|
parsed_url = urlparse(url) |
|
|
|
return parsed_url.netloc if parsed_url.netloc else parsed_url.path |
|
|
|
|
|
def get_ip_address(url): |
|
try: |
|
domain = get_domain(url) |
|
ip = socket.gethostbyname(domain) |
|
return ip |
|
except socket.gaierror: |
|
return None |
|
|
|
|
|
def get_country_info(ip): |
|
try: |
|
|
|
response = requests.get(f"https://ipinfo.io/{ip}/json") |
|
data = response.json() |
|
country_name = data.get("country", "Unknown") |
|
return country_name |
|
except requests.exceptions.RequestException as e: |
|
st.error(f"Error: {e}") |
|
return None |
|
|
|
|
|
def get_country_flag(country_name): |
|
try: |
|
|
|
country = pycountry.countries.get(name=country_name) |
|
if country: |
|
country_code = country.alpha_2 |
|
flag_url = f"https://countryflagsapi.com/png/{country_code.lower()}" |
|
return flag_url |
|
else: |
|
return "https://countryflagsapi.com/png/unknown" |
|
except AttributeError: |
|
return "https://countryflagsapi.com/png/unknown" |
|
|
|
|
|
def main(): |
|
st.title("Website IP and Country Info Finder") |
|
|
|
|
|
url = st.text_input("Enter the URL of the website:") |
|
|
|
|
|
if st.button("Submit"): |
|
if url: |
|
ip_address = get_ip_address(url) |
|
if ip_address: |
|
country_name = get_country_info(ip_address) |
|
if country_name: |
|
st.success(f"IP Address: {ip_address}") |
|
st.success(f"Country: {country_name}") |
|
flag_url = get_country_flag(country_name) |
|
st.image(flag_url, caption=f"Flag of {country_name}", width=100) |
|
else: |
|
st.error("Could not retrieve country information.") |
|
else: |
|
st.error("Could not resolve IP address from the given URL.") |
|
else: |
|
st.error("Please enter a valid URL.") |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|