-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #845 from alexwlchan/more-tils
Add a TIL that didn't get migrated across properly
- Loading branch information
Showing
2 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/_til/2024/2024-01-19-offline-geo-lookups-of-ip-addresses.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
layout: til | ||
title: How to do offline geo-lookups of IP addresses | ||
summary: | | ||
MaxMind offer databases you can do to look up IP addresses without sending the address off to a remote service. | ||
date: 2024-01-19 10:25:34 +0000 | ||
tags: | ||
- python | ||
- networking | ||
--- | ||
I was experimenting with web analytics, and I wanted a way to look up the country for an IP address -- in a privacy-preserving way. | ||
|
||
There are lots of web APIs for doing IP address lookup, e.g. | ||
|
||
```console | ||
$ curl "https://api.ipregistry.co/1.2.3.4?key=tryout" | ||
``` | ||
|
||
These are suitable for certain one-off tasks, but you're sending the IP address off to a third-party service. | ||
If I use this in an analytics package, I'm handing a complete list of visitor addresses to this service. | ||
Ick! | ||
|
||
(Plus making an HTTP request for each IP address probably introduces lots of latency.) | ||
|
||
A [Stack Overflow answer][so] pointed me at [MaxMind]. | ||
I was able to download a free country database from their site, which is about 6.4MB in "MaxMind DB" format -- a [database format][mmdb] designed for fast IP address lookups. | ||
|
||
I can then use [the maxminddb Python library][maxminddb] to open the database and look up IP addresses: | ||
|
||
```python | ||
import maxminddb | ||
|
||
with maxminddb.open_database('GeoLite2-Country_20240116/GeoLite2-Country.mmdb') as reader: | ||
print(reader.get('52.85.118.55')) | ||
# {'continent': {'code': 'NA', 'geoname_id': 6255149, … | ||
``` | ||
|
||
Note that this method can sometimes return `None`, if the IP address isn't in the database -- or in this case, if it's an IP address reserved for testing purposes. | ||
|
||
```python | ||
with maxminddb.open_database('GeoLite2-Country_20240116/GeoLite2-Country.mmdb') as reader: | ||
print(reader.get('192.0.2.0')) | ||
# None | ||
``` | ||
|
||
[so]: https://stackoverflow.com/q/17182203/1558022 | ||
[MaxMind]: https://www.maxmind.com/en/home | ||
[mmdb]: https://maxmind.github.io/MaxMind-DB/ | ||
[maxminddb]: https://pypi.org/project/maxminddb/ |
This file contains 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
dda32c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://alexwlchan.net as production
🚀 Deployed on https://665316594b92a6528291a169--alexwlchan.netlify.app