Skip to content

Instantly share code, notes, and snippets.

@Raykeymas
Created March 29, 2018 12:57
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 Raykeymas/e1a422ee8b4b9a37fdde181119d841de to your computer and use it in GitHub Desktop.
Save Raykeymas/e1a422ee8b4b9a37fdde181119d841de to your computer and use it in GitHub Desktop.
#必要なライブラリのインポート
from flask import Flask
from flask_httpauth import HTTPBasicAuth #HTTP"Basic"Auth
#Flask、HTTPBasicAuthクラスのインスタンスを作成
app = Flask(__name__)
auth = HTTPBasicAuth()
#"id":"パスワード"
id_list = {
"Tanaka": "1111",
"Suzuki": "1234"
}
#入力されたidに該当するパスワードを
#比較のために取得する
@auth.get_password
def get_pw(id):
if id in id_list:
return id_list.get(id)
return None
#実際の処理部分
@app.route('/')
@auth.login_required #ここで認証が行われる
#認証に成功したら以下の処理を実行する
def index():
return "Hello, %s!" % auth.username()
if __name__ == '__main__':
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment