I referred to this.
python
| #Import required libraries|
|:--|
| from flask import Flask |
| from flask_httpauth import HTTPBasicAuth #HTTP"Basic"Auth |
| |
| #Flask creates an instance of HTTPBasicAuth class|
| app = Flask(__name__) |
| auth = HTTPBasicAuth() |
| |
| #"id":"password" |
| id_list = { |
| "Tanaka": "1111", |
| "Suzuki": "1234" |
| } |
| |
| #Enter the password corresponding to the entered id|
| #Get for comparison|
| @auth.get_password |
| def get_pw(id): |
| if id in id_list: |
| return id_list.get(id) |
| return None |
| |
| #Actual processing part|
| @app.route('/') |
| @auth.login_required #Authentication is done here|
| #If the authentication is successful, execute the following process|
| def index(): |
| return "Hello, %s!" % auth.username() |
| |
| if __name__ == '__main__': |
| app.run() |code
Something was already working, so ① After importing the required library
@app.route('/')
Just before the processing part of ↑
@auth.login_required
To sandwich.
(2) Correct the ID / PASS to any one.
Raykeymas/flask-httpauth-basic.py
Recommended Posts