2016年11月19日土曜日

開発環境

Introducing Python: Modern Computing in Simple Packages (Bill Lubanovic (著)、 O'Reilly Media)のChapter 9(The Web, Untangled)、Things to Do 9.1-5.を取り組んでみる。

Things to Do 9.1-5.

コード(Emacs)

app.py

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

from flask import Flask, render_template, request

app = Flask(__name__)


@app.route('/')
def home():
    return "It's alive!"


@app.route('/home/')
def home1():
    thing = request.args.get('thing')
    height = request.args.get('height')
    color = request.args.get('color')
    return render_template('home.html',
                           thing=thing, height=height, color=color)

app.run(debug=True)

client.py

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

import requests

f = open('client.log', 'w')
url = 'http://localhost:5000'


def get_url(url):
    print(requests.get(url).text, file=f)

for u in [url, '{0}/home'.format(url),
          '{0}/home?thing=thing0&height=height0&color=color0'.format(url)]:
    print(u, file=f)
    get_url(u)

入出力結果(Terminal, IPython)

$ ./app.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 238-692-058
127.0.0.1 - - [19/Nov/2016 17:18:32] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [19/Nov/2016 17:18:32] "GET /home HTTP/1.1" 301 -
127.0.0.1 - - [19/Nov/2016 17:18:32] "GET /home/ HTTP/1.1" 200 -
127.0.0.1 - - [19/Nov/2016 17:18:32] "GET /home?thing=thing0&height=height0&color=color0 HTTP/1.1" 301 -
127.0.0.1 - - [19/Nov/2016 17:18:32] "GET /home/?thing=thing0&height=height0&color=color0 HTTP/1.1" 200 -
  C-c C-c$ cat client.log 
http://localhost:5000
It's alive!
http://localhost:5000/home
<!doctype html>
<html>
  <head>
    <title>It's alive!</title>
  </head>
  <body>
    I'm of course referring to None, which is None feet tall and
    None.
  </body>
</html>
http://localhost:5000/home?thing=thing0&height=height0&color=color0
<!doctype html>
<html>
  <head>
    <title>It's alive!</title>
  </head>
  <body>
    I'm of course referring to thing0, which is height0 feet tall and
    color0.
  </body>
</html>
$ 

0 コメント:

コメントを投稿