Skip to content

Instantly share code, notes, and snippets.

@inaniwa3
Created March 22, 2020 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inaniwa3/a268df39bac657981a7a19f8423eb76c to your computer and use it in GitHub Desktop.
Save inaniwa3/a268df39bac657981a7a19f8423eb76c to your computer and use it in GitHub Desktop.
from flask import Flask, request, abort, render_template
import geoip2.database
app = Flask(__name__)
# https://dev.maxmind.com/geoip/geoip2/geolite2/
reader = geoip2.database.Reader('./GeoLite2-City.mmdb')
@app.before_request
def before_request():
remote_addr = request.headers.get('X-Forwarded-For')
response = None
try:
response = reader.city(remote_addr)
except:
return abort(403)
name = ''
if response.subdivisions:
name = response.subdivisions.most_specific.names.get('ja')
if name != '秋田県':
return abort(403)
return
@app.route('/')
def route():
return render_template('akita.html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment