2017年3月13日月曜日

開発環境

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

EXERCISE(No. 4920)

コード(Emacs)

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

from flask import Flask, render_template, request, escape

import vsearch

app = Flask(__name__)


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


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


@app.route('/search4', methods=['POST'])
def do_search() -> 'html':
    phrase = request.form['phrase']
    letters = request.form['letters']
    title = 'Here are your result:'
    results = str(vsearch.search4letters(phrase, letters))
    log_request(request, results)
    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 open('vsearch.log') as f:
        contents = f.read()
    return escape(contents)

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

入出力結果(Terminal, IPython)

$ ./vsearch4log.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 873-014-066
127.0.0.1 - - [13/Mar/2017 19:51:17] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [13/Mar/2017 19:51:17] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [13/Mar/2017 19:51:22] "POST /search4 HTTP/1.1" 200 -
127.0.0.1 - - [13/Mar/2017 19:51:26] "GET /search4 HTTP/1.1" 405 -
127.0.0.1 - - [13/Mar/2017 19:51:27] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [13/Mar/2017 19:51:27] "GET /viewlog HTTP/1.1" 200 -
127.0.0.1 - - [13/Mar/2017 19:51:28] "GET /viewlog HTTP/1.1" 200 -
  C-c C-c$ cat vsearch.log 
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_get_file_stream', '_get_stream_for_parsing', '_is_old_module', '_load_form_data', '_parse_content_type', '_parsed_content_type', 'accept_charsets', 'accept_encodings', 'accept_languages', 'accept_mimetypes', 'access_route', 'application', 'args', 'authorization', 'base_url', 'blueprint', 'cache_control', 'charset', 'close', 'content_encoding', 'content_length', 'content_md5', 'content_type', 'cookies', 'data', 'date', 'dict_storage_class', 'disable_data_descriptor', 'encoding_errors', 'endpoint', 'environ', 'files', 'form', 'form_data_parser_class', 'from_values', 'full_path', 'get_data', 'get_json', 'headers', 'host', 'host_url', 'if_match', 'if_modified_since', 'if_none_match', 'if_range', 'if_unmodified_since', 'input_stream', 'is_json', 'is_multiprocess', 'is_multithread', 'is_run_once', 'is_secure', 'is_xhr', 'json', 'list_storage_class', 'make_form_data_parser', 'max_content_length', 'max_form_memory_size', 'max_forwards', 'method', 'mimetype', 'mimetype_params', 'module', 'on_json_loading_failed', 'parameter_storage_class', 'path', 'pragma', 'query_string', 'range', 'referrer', 'remote_addr', 'remote_user', 'routing_exception', 'scheme', 'script_root', 'shallow', 'stream', 'trusted_hosts', 'url', 'url_charset', 'url_root', 'url_rule', 'user_agent', 'values', 'view_args', 'want_form_data_parsed'] {'a', 'e', 'i', 'o'}
$ 

0 コメント:

コメントを投稿