Restriction IP avec flask \ -Qiita
Il y en avait, mais je l'ai mis avec un masque.
from flask import Flask, request, abort
import ipaddress
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
ALLOW_NETWORKS = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "127.0.0.1"]
@app.before_request
def before_request():
remote_addr = ipaddress.ip_address(request.remote_addr)
app.logger.info(remote_addr)
for allow_network in ALLOW_NETWORKS:
ip_network = ipaddress.ip_network(allow_network)
if remote_addr in ip_network:
app.logger.info(ip_network)
return
return abort(403, 'access denied from your IP address')
if __name__ == '__main__':
app.run()
Recommended Posts