[PYTHON] The day when basic authentication was added to the service operated by flask

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

What i did

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.

reference

Raykeymas/flask-httpauth-basic.py

Recommended Posts

The day when basic authentication was added to the service operated by flask
The day when the flask server running on linux on AWS was semi-persistent (running in the background)
Flask Basic authentication
Workaround when Authentication Required is displayed when trying to view the HDD by USB booting Linux
How to test the attributes added by add_request_method of pyramid
Change the installation destination when --user is added to pip
The record I was addicted to when putting MeCab on Heroku
The story that the guard was confined when the laboratory was converted to IoT
[Python] When the priority is the same in Priority Queue, it can be acquired in the order in which it was added to the queue.