Skip to content

Instantly share code, notes, and snippets.

@holly
Created July 28, 2024 13:35
Show Gist options
  • Save holly/a7b3b854edc710b7afb5068c55772201 to your computer and use it in GitHub Desktop.
Save holly/a7b3b854edc710b7afb5068c55772201 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import sys
import urllib
import io
import tarfile
import re
GEOIP_LICENSE_KEY = "your maxmind geoip license key"
method = "GET"
headers = { "User-Agent": "hello/1.0" }
for edition_id in [ "GeoLite2-ASN", "GeoLite2-City", "GeoLite2-Country" ]:
url = "https://download.maxmind.com/app/geoip_download?edition_id={}&license_key={}&suffix=tar.gz".format(edition_id, GEOIP_LICENSE_KEY)
download_file = edition_id + ".mmdb"
req = urllib.request.Request(url, method=method, headers=headers)
with urllib.request.urlopen(req) as res:
tar_gz_file = io.BytesIO(res.read())
with tarfile.open(fileobj=tar_gz_file, mode="r:gz") as tar:
for member in tar.getmembers():
p = re.compile(r'.+\/.*\.mmdb$')
if not p.match(member.name):
continue
with tar.extractfile(member) as f:
with open(download_file, "wb") as outfile:
outfile.write(f.read())
print("save {}".format(download_file))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment