開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Python 3.6 (プログラミング言語)
Head First Python (Paul Barry (著)、O'Reilly Media)のChapter 10.(Function Decorators: Wrapping Functions) の SHARPEN YOUR PENCIL(No. 7593) を取り組んでみる。
SHARPEN YOUR PENCIL(No. 7593)
コード(Emacs)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from flask import Flask, session app = Flask(__name__) app.secret_key = 'YouWillNeverGuess' @app.route('/login') def do_login() -> str: session['logged_in'] = True return 'You are now logged in' @app.route('/logout') def do_logout() -> str: if 'logged_in' in session: del session['logged_in'] return 'You are now logged out' @app.route('/status') def check_status() -> str: if 'logged_in' in session: return 'You are currently logged in' return 'You are NOT logged in' def check_logged_in(func): def wrapper(*args, **kwargs): if 'logged_in' in session: return func(*args, **kwargs) return 'You are NOT logged in' return wrapper if __name__ == '__main__': app.run(debug=True)
入出力結果(Terminal, IPython)
$ ./sample5.py * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 873-014-066 127.0.0.1 - - [12/Apr/2017 16:48:37] "GET /status HTTP/1.1" 200 - 127.0.0.1 - - [12/Apr/2017 16:48:37] "GET /favicon.ico HTTP/1.1" 404 - 127.0.0.1 - - [12/Apr/2017 16:48:43] "GET /login HTTP/1.1" 200 - 127.0.0.1 - - [12/Apr/2017 16:48:43] "GET /login HTTP/1.1" 200 - 127.0.0.1 - - [12/Apr/2017 16:48:47] "GET /status HTTP/1.1" 200 - 127.0.0.1 - - [12/Apr/2017 16:48:48] "GET /status HTTP/1.1" 200 - 127.0.0.1 - - [12/Apr/2017 16:48:51] "GET /login HTTP/1.1" 200 - 127.0.0.1 - - [12/Apr/2017 16:48:51] "GET /logout HTTP/1.1" 200 - 127.0.0.1 - - [12/Apr/2017 16:48:52] "GET /logout HTTP/1.1" 200 - 127.0.0.1 - - [12/Apr/2017 16:48:55] "GET /status HTTP/1.1" 200 - 127.0.0.1 - - [12/Apr/2017 16:48:55] "GET /status HTTP/1.1" 200 - C-c C-c$
0 コメント:
コメントを投稿