2018年12月26日水曜日

開発環境

Head First Python 第2版 ―頭とからだで覚えるPythonの基本 (Paul Barry (著)、嶋田 健志 (監修)、木下 哲也 (翻訳)、オライリージャパン)の11章(例外処理 - うまくいかないときに行うこと)、自分で考えてみよう(p. 435)を取り組んでみる。

コード(Emacs)

Python 3

from flask import Flask, render_template, request, escape
import vsearch
import psycopg2 as sql
from DBcm import UseDatabase

app = Flask(__name__)

dbconfig = {'host': '127.0.0.1',
            'database': 'vsearchlogdb'}


def log_request(req: 'flask_request', res: str) -> None:
    with UseDatabase(dbconfig) as cursor:
        _sql = '''
select phrase, letters, ip, browser_string, results
from log
'''
        cursor.execute(_sql)
        contents = cursor.fetchall()

    titles = ('phrase', 'letters', 'ユーザーエージェント', '結果')
    return render_template('viewlog.html',
                           the_title='ログの閲覧',
                           the_row_titles=titles,
                           the_data=contents,)
    conn.close()


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


@app.route('/search4', methods=['POST'])
def do_search() -> str:
    phrase = request.form['phrase']
    letters = request.form['letters']
    results = str(vsearch.search4letters(phrase, letters))
    try:
        log_request(request, results)
    except Exception as err:
        print(type(err), err)
    title = '検索結果'
    return render_template(
        'results.html',
        the_title=title,
        the_phrase=phrase,
        the_letters=letters,
        the_results=results)


@app.route('/viewlog')
def view_the_log() -> str:
    with UseDatabase(dbconfig) as cursor:
        _sql = '''
select phrase, letters, ip, browser_string, results
from log
'''
        cursor.execute(_sql)
        contents = cursor.fetchall()

    titles = ('phrase', 'letters', 'ユーザーエージェント', '結果')
    return render_template('viewlog.html',
                           the_title='ログの閲覧',
                           the_row_titles=titles,
                           the_data=contents,)


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

入出力結果(Terminal, cmd(コマンドプロンプト), Jupyter(IPython))

$ python3 hello_flask.py
 * Serving Flask app "hello_flask" (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: 288-710-193
127.0.0.1 - - [26/Dec/2018 11:36:12] "GET /search4 HTTP/1.1" 405 -
127.0.0.1 - - [26/Dec/2018 11:36:13] "GET /favicon.ico HTTP/1.1" 404 -
 * Detected change in '/.../webapp/hello_flask.py', reloading
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 288-710-193
 * Detected change in '/.../webapp/hello_flask.py', reloading
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 288-710-193
 * Detected change in '/.../webapp/hello_flask.py', reloading
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 288-710-193
127.0.0.1 - - [26/Dec/2018 11:37:12] "GET /search4 HTTP/1.1" 405 -
127.0.0.1 - - [26/Dec/2018 11:37:13] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2018 11:37:13] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2018 11:37:13] "GET /static/hf.css HTTP/1.1" 404 -
127.0.0.1 - - [26/Dec/2018 11:37:13] "GET /static/hf.css HTTP/1.1" 404 -
<class 'psycopg2.OperationalError'> could not connect to server: Connection refused
 Is the server running on host "127.0.0.1" and accepting
 TCP/IP connections on port 5432?

127.0.0.1 - - [26/Dec/2018 11:37:17] "POST /search4 HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2018 11:37:17] "GET /static/hf.css HTTP/1.1" 404 -
<class 'psycopg2.ProgrammingError'> column "result" does not exist
LINE 2: select phrase, letters, ip, browser_string, result
                                                    ^
HINT:  Perhaps you meant to reference the column "log.results".

127.0.0.1 - - [26/Dec/2018 11:37:49] "POST /search4 HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2018 11:37:49] "GET /static/hf.css HTTP/1.1" 404 -
 * Detected change in '/.../webapp/hello_flask.py', reloading
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 288-710-193
127.0.0.1 - - [26/Dec/2018 11:38:27] "POST /search4 HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2018 11:38:27] "GET /static/hf.css HTTP/1.1" 404 -
  C-c C-c$

0 コメント:

コメントを投稿