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
- All Categories
- 179 LFX Mentorship
- 179 LFX Mentorship: Linux Kernel
- 776 Linux Foundation IT Professional Programs
- 384 Cloud Engineer IT Professional Program
- 175 Advanced Cloud Engineer IT Professional Program
- 76 DevOps IT Professional Program - Discontinued
- 7 DevOps & GitOps IT Professional Program
- 103 Cloud Native Developer IT Professional Program
- 7.7K Training Courses & Learning Paths
- 17 AI & ML Training
- 2 Blockchain & Decentralized Identity Training
- 44 Cloud & Containers Training
- 7 Cybersecurity Training
- 4 DevOps & Site-Reliability Training
- 3 Linux Kernel Development Training
- 2 Networking Training
- 2 Open Source Best Practice Training
- 6 System Administration Training
- 1 System Engineering Training
- 6 Web & Application Development Training
- 798 Hardware
- 202 Drivers
- 68 I/O Devices
- 37 Monitors
- 96 Multimedia
- 173 Networking
- 91 Printers & Scanners
- 92 Storage
- 774 Linux Distributions
- 82 Debian
- 68 Fedora
- 24 Linux Mint
- 13 Mageia
- 24 openSUSE
- 151 Red Hat Enterprise
- 32 Slackware
- 13 SUSE Enterprise
- 356 Ubuntu
- 473 Linux System Administration
- 31 Cloud Computing
- 73 Command Line/Scripting
- Github systems admin projects
- 104 Linux Security
- 79 Network Management
- 102 System Management
- 46 Web Management
- 174 Mobile Computing
- 32 Android
- 127 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 407 Off Topic
- 128 Introductions
- 35 Study Material
- 1.1K Programming and Development
- 312 Kernel Development
- 729 Software Development
- 1K Software
- 421 Applications
- 183 Command Line
- 5 Compiling/Installing
- 71 Games
- 320 Installation
- Archived
- 183 Small Talk
- 2 LFD140 Class Forum
- 1.4K LFS258 Class Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)