開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
- Flask (パッケージ)
Head First Python 第2版 ―頭とからだで覚えるPythonの基本 (Paul Barry (著)、嶋田 健志 (監修)、木下 哲也 (翻訳)、オライリージャパン)の10章(関数デコレーター - 関数を包む)、自分で考えてみよう(p. 377)を取り組んでみる。
コード(Emacs)
Python 3
#!/usr/bin/env python3 from flask import Flask, session app = Flask(__name__) @app.route('/login') def do_login() -> str: session['logged_in'] = True return '現在ログインしています。' @app.route('/logout') def do_logout() -> str: session.pop('logged_in') return '現在ログアウトしています。' @app.route('/status') def chec_status() -> str: if 'logged_in' in session: return '現在ログインしています。' return 'ログインしていません。' app.secret_key = 'you will never guess' if __name__ == '__main__': app.run(debug=True)
入出力結果(Terminal, cmd(コマンドプロンプト), Jupyter(IPython))
$ python3 sample2.py * Serving Flask app "sample2" (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 - - [15/Dec/2018 16:44:03] "GET /status HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:04] "GET /status HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:06] "GET / HTTP/1.1" 404 - 127.0.0.1 - - [15/Dec/2018 16:44:07] "GET /login HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:07] "GET /login HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:10] "GET / HTTP/1.1" 404 - 127.0.0.1 - - [15/Dec/2018 16:44:10] "GET /status HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:10] "GET /status HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:13] "GET /login HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:14] "GET /logout HTTP/1.1" 500 - Traceback (most recent call last): File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2309, in __call__ return self.wsgi_app(environ, start_response) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2295, in wsgi_app response = self.handle_exception(e) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1741, in handle_exception reraise(exc_type, exc_value, tb) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise raise value File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app response = self.full_dispatch_request() File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise raise value File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request rv = self.dispatch_request() File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File ".../sample2.py", line 15, in do_logout sesseon.pop('logged_in') NameError: name 'sesseon' is not defined 127.0.0.1 - - [15/Dec/2018 16:44:14] "GET /logout HTTP/1.1" 500 - Traceback (most recent call last): File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2309, in __call__ return self.wsgi_app(environ, start_response) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2295, in wsgi_app response = self.handle_exception(e) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1741, in handle_exception reraise(exc_type, exc_value, tb) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise raise value File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app response = self.full_dispatch_request() File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise raise value File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request rv = self.dispatch_request() File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File ".../sample2.py", line 15, in do_logout sesseon.pop('logged_in') NameError: name 'sesseon' is not defined 127.0.0.1 - - [15/Dec/2018 16:44:14] "GET /logout?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:14] "GET /logout?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:14] "GET /logout?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:14] "GET /logout?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:14] "GET /logout?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:14] "GET /logout?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:14] "GET /logout?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:14] "GET /logout?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 - * Detected change in '.../sample2.py', reloading * Restarting with stat * Debugger is active! * Debugger PIN: 178-619-558 127.0.0.1 - - [15/Dec/2018 16:44:30] "GET /logout HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:30] "GET /logout?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:30] "GET /logout?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:30] "GET /logout?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:30] "GET /logout?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:30] "GET /logout?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:34] "GET /status HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:34] "GET /status HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:37] "GET /login HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:37] "GET /login HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:40] "GET /status HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:40] "GET /status HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:43] "GET /login HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:43] "GET /logout?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:43] "GET /logout?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:43] "GET /logout?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:43] "GET /logout?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:43] "GET /logout?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:43] "GET /logout HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:46] "GET /search4 HTTP/1.1" 404 - 127.0.0.1 - - [15/Dec/2018 16:44:46] "GET /favicon.ico HTTP/1.1" 404 - 127.0.0.1 - - [15/Dec/2018 16:44:46] "GET /status HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:47] "GET /status HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:49] "GET /login HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:50] "GET /logout HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:50] "GET /logout?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:50] "GET /logout?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:50] "GET /logout?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:50] "GET /logout?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 - 127.0.0.1 - - [15/Dec/2018 16:44:50] "GET /logout?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 - C-c C-c$
0 コメント:
コメントを投稿