2017年7月27日木曜日

開発環境

Head First Python (Paul Barry (著)、O'Reilly Media)のChapter 6.(Storing and Manipulating Data: Where to Put Your Data)の EXERCISE(No. 4834) を取り組んでみる。

EXERCISE(No. 4834)

コード(Emacs)

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

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

app = Flask(__name__)


@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))
    log_request(request, results)
    return render_template('results.html',
                           the_title=title,
                           the_phrase=phrase,
                           the_results=results,
                           the_letters=letters)


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


def log_request(req: 'flask_request', res: str) -> None:
    with open('vsearch.log', 'a') as f:
        print(req, res, file=f)

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 - - [27/Jul/2017 14:49:22] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [27/Jul/2017 14:49:22] "GET /static/hf.css HTTP/1.1" 404 -
127.0.0.1 - - [27/Jul/2017 14:49:25] "POST /search4 HTTP/1.1" 200 -
127.0.0.1 - - [27/Jul/2017 14:49:25] "GET /static/hf.css HTTP/1.1" 404 -
127.0.0.1 - - [27/Jul/2017 14:49:31] "POST /search4 HTTP/1.1" 200 -
127.0.0.1 - - [27/Jul/2017 14:49:31] "GET /static/hf.css HTTP/1.1" 404 -
  C-c C-c$ cat vsearch.log 
<Request 'http://127.0.0.1:5000/search4' [POST]> set()
<Request 'http://127.0.0.1:5000/search4' [POST]> {'o'}
$

0 コメント:

コメントを投稿

Comments on Google+: