Maps and Geolocation Data
Plot IP geolocation data on an interactive map for easy visualization.
If you have server logs with 100+ entries and want to visualize where requests originated geographically, follow these steps:
Extract IP addresses from log entries
Assuming Apache Common Log Format, where the first field is the client IP address:
ip_addresses = []
# Populate an array with IPs of interest
with open('access.log', 'r') as file:
for line in file:
ip = line.split()[0]
ip_addresses.append(ip)Query the Fastah API and build an IP-to-coordinates dictionary
For each unique IP, fetch geolocation data and store the results:
ip_location_dictionary = {}
# Query Fastah API for each IP and build a dictionary of IP addresses to coordinates
for ip in ip_addresses:
if ip not in ip_location_dictionary.get("ip",""):
# Build API request
url = f"https://ep.api.getfastah.com/whereis/v1/json/{ip}"
headers = {"Fastah-Key": FASTAH_API_KEY}
# Fetch geolocation data
response = requests.get(url, headers=headers)
data = response.json()
# Extract coordinates from response
ip_address = data["ip"]
latitude = data["locationData"]["lat"]
longitude = data["locationData"]["lng"]
ip_location_dictionary[ip_address] = {"latitude": latitude, "longitude": longitude}Render markers on an interactive map
Use the folium Python library that wraps Leaflet.js maps to create clustered markers. Or substitute Google Maps or Mapbox).
# Initialize map centered at world view with zoom level 2
geolocation_map = folium.Map(location=[0, 0], zoom_start=2)
# Add marker cluster group to organize multiple location points
marker_cluster = MarkerCluster().add_to(geolocation_map)
# Add individual markers for each IP location
for ip_address, coordinates in ip_location_dictionary.items():
folium.Marker(
location=[coordinates["latitude"], coordinates["longitude"]],
popup=ip_address
).add_to(marker_cluster)
# Save interactive map to HTML file
geolocation_map.save("ip_map.html")Updated 2 days ago
What’s Next
