Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import ipaddress | |
| def ip_lookup(ip_address): | |
| try: | |
| # Validate and check if it's a private IP | |
| ip_obj = ipaddress.ip_address(ip_address) | |
| if ip_obj.is_private: | |
| return "⚠️ This is a private/local IP address. Lookup not available." | |
| response = requests.get(f"http://ip-api.com/json/{ip_address}") | |
| data = response.json() | |
| if data["status"] == "fail": | |
| return f"❌ Error: {data.get('message', 'Invalid IP address')}" | |
| result = f""" | |
| **IP Address**: {data.get('query', 'N/A')} | |
| **Country**: {data.get('country', 'N/A')} | |
| **Region**: {data.get('regionName', 'N/A')} | |
| **City**: {data.get('city', 'N/A')} | |
| **ZIP**: {data.get('zip', 'N/A')} | |
| **Latitude/Longitude**: {data.get('lat', 'N/A')}, {data.get('lon', 'N/A')} | |
| **Timezone**: {data.get('timezone', 'N/A')} | |
| **ISP**: {data.get('isp', 'N/A')} | |
| **Organization**: {data.get('org', 'N/A')} | |
| """ | |
| return result.strip() | |
| except ValueError: | |
| return "❌ Invalid IP address format." | |
| except Exception as e: | |
| return f"❌ Error: {str(e)}" | |
| iface = gr.Interface( | |
| fn=ip_lookup, | |
| inputs=gr.Textbox(label="Enter IP Address", placeholder="e.g. 8.8.8.8"), | |
| outputs=gr.Markdown(label="IP Lookup Result"), | |
| title="IP Lookup Tool", | |
| description="Enter a public IP address to get geolocation, ISP, organization, and more." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |