Welcome to the Linux Foundation Forum!

Where is my IP address location, really? Building accurate IP geolocation into a Python backend

I'm building location-aware features into a Python backend and I've hit the limits of trusting the client to tell me where it is.

The requirement: geo-restrictions, fraud scoring, and default language/currency selection all need the visitor's location derived from their IP, not from browser settings or self-reported account data.

Step 1 was getting the actual client IP behind our reverse proxy, since naive remote_addr just returns the load balancer:

def get_client_ip(request): forwarded = request.headers.get("X-Forwarded-For", "") return forwarded.split(",")[0].strip() if forwarded else request.remote_addr

Step 2 was resolving that IP locally with GeoLite2:

import geoip2.database

reader = geoip2.database.Reader("GeoLite2-City.mmdb")city = reader.city("203.0.113.45").city.name

The gaps I've run into:

City-level accuracy is acceptable for residential IPs, but a lot of mobile traffic resolves to the carrier's regional hub, occasionally in a different state entirely
Datacenter and VPN IPs either resolve to the datacenter location or return nothing useful, which poisons fraud scoring in both directions
The free database refreshes weekly, so freshly allocated ranges return empty until the next drop
Some IPv6 ranges are patchy compared to IPv4 coverage
End users can type whats my ip address location into any search engine and get an instant city-level answer with ISP and ASN attached. Reproducing that reliability server-side is apparently not trivial.

So how do services that answer where is my ip address location in real time actually work? Are they maintaining their own allocation datasets, licensing commercial feeds, or resolving each lookup through an upstream API? And for the VPN/datacenter case specifically: do production backends treat a non-residential IP as "unknown location" and fall back to account settings, or do you attempt deeper detection?

Context: signup flow and content gating, a few thousand lookups per day, Python, self-hosted stack on Linux.

Categories

Upcoming Training