make geoip2 optional

This commit is contained in:
Hector Martin 2021-04-03 15:49:34 +09:00
parent 0130c51c2c
commit 93c15a22c9

12
app.py
View File

@ -1,7 +1,6 @@
import os, zipfile, hashlib, hmac, struct, logging, random, json import os, zipfile, hashlib, hmac, struct, logging, random, json
import urllib import urllib
from io import BytesIO from io import BytesIO
import geoip2.database, geoip2.errors
from logging.handlers import SMTPHandler from logging.handlers import SMTPHandler
from datetime import datetime, timedelta from datetime import datetime, timedelta
from flask import Flask, request, g, render_template, make_response, redirect, url_for from flask import Flask, request, g, render_template, make_response, redirect, url_for
@ -17,14 +16,13 @@ TEMPLATES = {
} }
BUNDLEBASE = os.path.join(app.root_path, 'bundle') BUNDLEBASE = os.path.join(app.root_path, 'bundle')
#BUNDLE = [(name, os.path.join(BUNDLEBASE,name)) for name in os.listdir(BUNDLEBASE)]
#OUI_LIST = [i.decode('hex') for i in open(os.path.join(app.root_path, 'oui_list.txt')).read().split("\n") if len(i)==6]
COUNTRY_REGIONS = dict([l.split(" ") for l in open(os.path.join(app.root_path, 'country_regions.txt')).read().split("\n") if l]) COUNTRY_REGIONS = dict([l.split(" ") for l in open(os.path.join(app.root_path, 'country_regions.txt')).read().split("\n") if l])
#gi = pygeoip.GeoIP(os.path.join(app.root_path, 'GeoIP.dat')) try:
import geoip2.database, geoip2.errors
gi = geoip2.database.Reader('/usr/share/GeoIP/GeoLite2-Country.mmdb') gi = geoip2.database.Reader('/usr/share/GeoIP/GeoLite2-Country.mmdb')
except ImportError:
gi = None
class RequestFormatter(logging.Formatter): class RequestFormatter(logging.Formatter):
def format(self, record): def format(self, record):
@ -50,6 +48,8 @@ if not app.debug:
app.logger.warning('Starting...') app.logger.warning('Starting...')
def region(): def region():
if gi is None:
return 'E'
try: try:
country = gi.country(request.remote_addr).country.iso_code country = gi.country(request.remote_addr).country.iso_code
app.logger.info("GI: %s -> %s", request.remote_addr, country) app.logger.info("GI: %s -> %s", request.remote_addr, country)