2018年10月7日日曜日

開発環境

  • macOS Mojave - Apple
  • Emacs (Text Editor)
  • Python 3.7 (プログラミング言語)

Head First Python 第2版 ―頭とからだで覚えるPythonの基本 (Paul Barry (著)、嶋田 健志 (監修)、木下 哲也 (翻訳)、オライリージャパン)の6章(データの格納と操作 - データをファイルに格納する)、エクササイズ(p. 264)を取り組んでみる。

PythonAnywhere

コード(Emacs)

Python 3

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 log:
        print(req.form, req.remote_addr, req.user_agent, res,
              file=log, sep='|')


@app.route('/search4', methods=['POST'])
def do_search():
    phrase = request.form['phrase']
    letters = request.form['letters']
    title = '検索結果'
    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('/')
@app.route('/entry')
def entry_page() -> str:
    return render_template(
        'entry.html', the_title='Web版のsearch4lettersにようこそ!')


@app.route('/viewlog')
def view_the_log() -> str:
    with open('vsearch.log') as log:
        contents = [[escape(col) for col in row.split('|')]
                    for row in log]
    return str(contents)


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

入出力結果(Terminal, Jupyter(IPython))

$ python3 vsearch4web.py
 * Serving Flask app "vsearch4web" (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: 178-619-558
  C-c C-z
[1]+  Stopped                 python3 vsearch4web.py
$ jobs
[1]+  Stopped                 python3 vsearch4web.py
$ bg 1
[1]+ python3 vsearch4web.py &
$ curl http://127.0.0.1:5000/viewlog
127.0.0.1 - - [07/Oct/2018 20:57:37] "GET /viewlog HTTP/1.1" 200 -
[[Markup('ImmutableMultiDict([('phrase', 'This is a test of the posting capability'), ('letters', 'aeiou')])'), Markup('127.0.0.1'), Markup('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Safari/605.1.15'), Markup('{'i', 'a', 'o', 'e'}\n')], [Markup('ImmutableMultiDict([('phrase', 'This is a test of the posting capability'), ('letters', 'AEIOU')])'), Markup('127.0.0.1'), Markup('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Safari/605.1.15'), Markup('set()\n')], [Markup('ImmutableMultiDict([('phrase', 'Head First Python'), ('letters', 'aeiou')])'), Markup('127.0.0.1'), Markup('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Safari/605.1.15'), Markup('{'i', 'a', 'o', 'e'}\n')], [Markup('ImmutableMultiDict([('phrase', 'Head First Python 2nd edition'), ('letters', 'aeiou')])'), Markup('127.0.0.1'), Markup('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Safari/605.1.15'), Markup('{'i', 'a', 'o', 'e'}\n')], [Markup('ImmutableMultiDict([('phrase', 'Head First Python 2nd Edition'), ('letters', 'aeiou')])'), Markup('127.0.0.1'), Markup('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Safari/605.1.15'), Markup('{'i', 'a', 'o', 'e'}\n')]]$ fg
python3 vsearch4web.py
  C-c C-c$ 

0 コメント:

コメントを投稿

関連コンテンツ