Created
July 2, 2025 14:15
-
-
Save ptaylor/329c87850ba670ace6309d18c63606da to your computer and use it in GitHub Desktop.
IP Map
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 folium | |
| import requests | |
| # Load IPs from a file | |
| with open('ips.txt', 'r') as file: | |
| ip_list = [line.strip() for line in file if line.strip()] | |
| # Create a world map centered at latitude 0, longitude 0 | |
| folium_map = folium.Map(location=[0, 0], zoom_start=2) | |
| # Loop through IPs and place markers | |
| for ip in ip_list: | |
| try: | |
| res = requests.get(f'https://ipinfo.io/{ip}/json', timeout=5).json() | |
| if 'loc' in res: | |
| loc = res['loc'] | |
| lat, lon = map(float, res['loc'].split(',')) | |
| city = res.get('city', 'Unknown') | |
| country = res.get('country', '') | |
| popup_info = f"{ip}\n{city, country}" | |
| folium.Marker([lat, lon], popup=popup_info).add_to(folium_map) | |
| print(f"{ip} -> {lat}, {lon} -> {city} {country}") | |
| except Exception as e: | |
| print(f"⚠️ Could not locate {ip}: {e}") | |
| # Save to HTML | |
| folium_map.save('ip_map.html') | |
| print("✅ Map generated and saved as ip_map.html") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment