[PYTHON] IP restrictions with Flask

IP restriction with flask -Qiita

There was, but I put it on with a mask.

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

IP restrictions with Flask
Hello world with flask
Programming with Python Flask
Deploy Flask with ZEIT Now
Touch Flask + run with Heroku
Hello World with Flask + Hamlish
Record global IP with python
Unit test flask with pytest
API with Flask + uWSGI + Nginx
SNS Flask (Ajax) made with Flask
Web application development with Flask
View flask coverage with pytest-cov
Web application with Python + Flask ② ③
File upload with Flask + jQuery
Web application with Python + Flask ④
SNS Flask (Model) edition made with Flask
[LINE login] Verify state with Flask
SNS Python basics made with Flask
[Memo] Links for developing with Flask
Creating a Flask server with Docker
Run the app with Flask + Heroku
Persist Flask API server with forever
[Python] Use Basic/Digest authentication with Flask
Creating a simple app with flask
Build Flask environment with Dockerfile + docker-compose.yml
flask
SNS made with Flask Flask (Blueprint, bcrypt)
flask
Post bulletin board creation with flask
Application development with Docker + Python + Flask
Image upload function with Vue.js + Flask
Azure table storage with PTVS Flask app
Deploy flask app with mod_wsgi (using pipenv)
I tried linebot with flask (anaconda) + heroku
Getting Started with Flask with Azure Web Apps
Vue.js + Flask environment construction memorandum ~ with Anaconda3 ~
Make a rare gacha simulator with Flask
POST variously with Python and receive with Flask
Page cache in Python + Flask with Flask-Caching
Send CSS compressed to Gzip with Flask
Get data from Cloudant with Bluemix flask
Create a simple web app with flask
Easy web app with Python + Flask + Heroku
[Python] Quickly create an API with Flask
Create a web service with Docker + Flask
Getting Started with Heroku, Deploying Flask App
Launch Flask application with Docker on Heroku
Supports iOS compatible streaming privately (with restrictions)