LoginSignup
26
26

More than 5 years have passed since last update.

PythonのbottleでBASIC認証

Last updated at Posted at 2014-05-21

bottle.auth_basicをデコレータとして使うとBASIC認証をかけられる。
v0.12以上が必要。

hello.py
# -*- coding: utf-8 -*-
import bottle


# BASIC認証のユーザ名とパスワード
USERNAME = "user"
PASSWORD = "pass"


def check(username, password):
    u"""
    BASIC認証のユーザ名とパスワードをチェック
    @bottle.auth_basic(check)で適用
    """
    return username == USERNAME and password == PASSWORD


@bottle.route("/hello")
@bottle.auth_basic(check)
def hello():
    return "hello"


if __name__ == '__main__':
    bottle.run(host='localhost', port=8080, debug=True)

実行

$ python hello.py
26
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
26