2018年10月14日日曜日

開発環境

  • macOS Mojave - Apple
  • Emacs (Text Editor)
  • Python 3.7 (プログラミング言語)

Head First Python 第2版 ―頭とからだで覚えるPythonの基本 (Paul Barry (著)、嶋田 健志 (監修)、木下 哲也 (翻訳)、オライリージャパン)の10章(関数デコレーター - 関数を包む)、自分で考えてみよう(p. 377)を取り組んでみる。

コード(Emacs)

Python 3

# 1. session を追加
from flask import Flask, session

app = Flask(__name__)

# 2. secret_keyを追加
app.secret_key = '秘密の鍵'


@app.route('/login')
def do_login() -> str:
    session['logged_in'] = True
    return '現在ログインしています。'


@app.route('/logout')
def do_logout() -> str:
    session.pop('logged_in', session)
    return '現在ログアウトしています。'


@app.route('/status')
def check_status() -> str:
    if 'logged_in' in session:
        return '現在ログインしています。'
    return 'ログインしていません。'


if __name__ == '__main__':
    app.run(debug=True)

入出力結果(Terminal, Jupyter(IPython))

$ python3 simple_webapp.py
 * Serving Flask app "simple_webapp" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 178-619-558
127.0.0.1 - - [14/Oct/2018 18:27:59] "GET /status HTTP/1.1" 200 -
127.0.0.1 - - [14/Oct/2018 18:28:04] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [14/Oct/2018 18:28:08] "GET /login HTTP/1.1" 200 -
127.0.0.1 - - [14/Oct/2018 18:28:08] "GET /logout HTTP/1.1" 200 -
127.0.0.1 - - [14/Oct/2018 18:28:15] "GET /status HTTP/1.1" 200 -
127.0.0.1 - - [14/Oct/2018 18:28:18] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [14/Oct/2018 18:28:19] "GET /login HTTP/1.1" 200 -
127.0.0.1 - - [14/Oct/2018 18:28:23] "GET /status HTTP/1.1" 200 -
  C-c C-c$

0 コメント:

コメントを投稿