Skip to content

Commit

Permalink
Merge pull request #845 from alexwlchan/more-tils
Browse files Browse the repository at this point in the history
Add a TIL that didn't get migrated across properly
  • Loading branch information
alexwlchan authored May 26, 2024
2 parents c4c25c4 + a0337fa commit dda32c4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
49 changes: 49 additions & 0 deletions src/_til/2024/2024-01-19-offline-geo-lookups-of-ip-addresses.md
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/
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: til
date: 2024-02-21 12:33:15 +0000
title: What happens when you replace a photo?
title: What happens when you replace a photo on Flickr?
tags:
- flickr
---
Expand Down Expand Up @@ -32,7 +32,7 @@ Here's an example from one of my photos, using the flickr.photos.getInfo API:
```

Note that this also bumps the `lastupdate` attribute in the `<dates>` element, e.g.:

```diff
<dates
posted="1707315985"
Expand Down

1 comment on commit dda32c4

@alexwlchan
Copy link
Owner Author

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

Please sign in to comment.