Login

Django management command to update local Maxmind Geoip databases

Author:
pakal
Posted:
June 26, 2019
Language:
Python
Version:
Not specified
Tags:
maxmind management databases geoip python3
Score:
0 (after 0 ratings)

This management command updates country and city Geolite databases from Maxmind (binary databases, not CSV ones), for use with Django's builtin Geoip utilities.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import io
import pathlib
import tarfile

import requests
from django.conf import settings
from django.core.management.base import BaseCommand

DATABASE_URLS = [
    (
        "GeoLite2-Country.mmdb",
        "https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz",
    ),
    (
        "GeoLite2-City.mmdb",
        "https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz",
    ),
]


class Command(BaseCommand):
    help = "Update Maxmind GeoLite binary databases for Cities and Countries."

    def handle(self, *args, **options):

        data_path = pathlib.Path(settings.GEOIP_PATH)
        data_path.mkdir(parents=True, exist_ok=True)

        for db_name, url in DATABASE_URLS:
            _, local_filename = url.rsplit("/", 1)
            local_fullpath = str(data_path / db_name)
            self.stdout.write(
                "Downloading geoip file '%s' to '%s'" % (url, local_fullpath)
            )

            try:
                req = requests.get(url, stream=True)
                fileobj = io.BytesIO(req.content)  # We need seekability
                tar = tarfile.open(
                    mode="r:gz",
                    fileobj=fileobj,
                    errorlevel=1,  # Thus all fatal errors are raised as OSError
                )

                all_member_names = tar.getnames()
                for member_name in all_member_names:
                    if member_name.endswith(db_name):

                        fr = tar.extractfile(member_name)
                        db_content = fr.read()
                        with open(local_fullpath, "wb") as fw:
                            fw.write(db_content)
                        self.stdout.write(
                            "New DB extracted from archive: %s" % member_name
                        )
                        break
                else:
                    raise IOError(
                        "Couldn't find Geoip DB '%s' inside archive: %s"
                        % (db_name, all_member_names)
                    )
            except EnvironmentError as exc:
                self.stderr.write("Download of Geoip DB failed: %r" % exc)
                raise

More like this

Comments

Please login first before commenting.