2017年8月13日日曜日

開発環境

Head First Python (Paul Barry (著)、O'Reilly Media)のChapter 10.(Function Decorators: Wrapping Functions)の SHARPEN YOUR PENCIL(No. 7191) を取り組んでみる。

SHARPEN YOUR PENCIL(No. 7191)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from flask import Flask, render_template, request, session
from vsearch import search4letters

app = Flask(__name__)

app.secret_key = 'YouWillNeverGuess'


@app.route('/')
def hello() -> str:
    return 'Hello world from Flask!'


@app.route('/search4', methods=['POST'])
def do_search() -> 'html':
    phrase = request.form['phrase']
    letters = request.form['letters']
    title = 'Here are your results:'
    results = str(search4letters(phrase, letters))
    return render_template('results.html',
                           the_title=title,
                           the_phrase=phrase,
                           the_results=results,
                           the_letters=letters)


@app.route('/entry')
def entry_page() -> 'html':
    return render_template('entry.html',
                           the_title='Welcome to search4letters on the web!')


@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'

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

入出力結果(Terminal, IPython)

$ ./vsearch4web.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 - - [13/Aug/2017 17:21:38] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:21:39] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:21:40] "GET /login HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:21:40] "GET /login HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:21:43] "GET /status HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:21:43] "GET /status HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:21:46] "GET /login HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:21:47] "GET /logout HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:21:47] "GET /logout HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:21:50] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:21:50] "GET /login HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:21:51] "GET /login HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:21:55] "GET /logout HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:21:55] "GET /logout HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:21:58] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:21:59] "GET /search4 HTTP/1.1" 405 -
127.0.0.1 - - [13/Aug/2017 17:21:59] "GET /status HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:21:59] "GET /status HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:22:02] "GET /login HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:22:03] "GET /login HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:22:06] "GET /status HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2017 17:22:06] "GET /status HTTP/1.1" 200 -
  C-c C-c$

0 コメント:

コメントを投稿