2017年8月17日木曜日

開発環境

Head First Python (Paul Barry (著)、O'Reilly Media)のChapter 11.(Exception Handling: What to Do When Things Go Wrong)の SHARPEN YOUR PENCIL(No. 8268) を取り組んでみる。

SHARPEN YOUR PENCIL(No. 8268)

コード(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))
    try:
        log_request(request, results)
    except Exception as err:
        print(type(err), err)
    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'


def log_request(req: 'flask_request', res: str) -> None:
    with UseDatabase(app.config['dbconfig']) as cursor:
        _SQL = '''insert into log
                  (phrase, letters, ip, browser_string, results)
                  values
                  (%s, %s, %s, %s, %s)'''
        cursor.execute(_SQL, (req.form['phrase'],
                              req.form['letters'],
                              req.remote_addr,
                              req.user_agent.browser,
                              res,))


@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 - - [17/Aug/2017 14:16:47] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [17/Aug/2017 14:16:50] "GET /entry HTTP/1.1" 200 -
127.0.0.1 - - [17/Aug/2017 14:16:50] "GET /static/hf.css HTTP/1.1" 404 -
127.0.0.1 - - [17/Aug/2017 14:16:50] "GET /entry HTTP/1.1" 200 -
127.0.0.1 - - [17/Aug/2017 14:16:50] "GET /static/hf.css HTTP/1.1" 404 -
<class 'NameError'> name 'UseDatabase' is not defined
127.0.0.1 - - [17/Aug/2017 14:16:52] "POST /search4 HTTP/1.1" 200 -
127.0.0.1 - - [17/Aug/2017 14:16:52] "GET /static/hf.css HTTP/1.1" 404 -
<class 'NameError'> name 'UseDatabase' is not defined
127.0.0.1 - - [17/Aug/2017 14:16:56] "POST /search4 HTTP/1.1" 200 -
127.0.0.1 - - [17/Aug/2017 14:16:56] "GET /static/hf.css HTTP/1.1" 404 -
<class 'NameError'> name 'UseDatabase' is not defined
127.0.0.1 - - [17/Aug/2017 14:17:01] "POST /search4 HTTP/1.1" 200 -
127.0.0.1 - - [17/Aug/2017 14:17:01] "GET /static/hf.css HTTP/1.1" 404 -
  C-c C-c$

0 コメント:

コメントを投稿