2016年8月22日月曜日

開発環境

入門 Python 3 (Bill Lubanovic (著)、 斎藤 康毅(監修)、 長尾 高弘 (翻訳)、オライリージャパン)の9章(ウェブを解きほぐす)、9.4(復習問題)を取り組んでみる。

コード(Emacs)

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

from flask import Flask
import flask

app = Flask(__name__, static_folder='.', static_url_path='')


@app.route('/home.html')
def home0():
    kwargs = {}
    kwargs['thing'] = flask.request.args.get('thing')
    kwargs['height'] = flask.request.args.get('height')
    kwargs['color'] = flask.request.args.get('color')
    return flask.render_template('home.html', **kwargs)


@app.route('/')
def home():
    kwargs = {}
    kwargs['thing'] = flask.request.args.get('thing')
    kwargs['height'] = flask.request.args.get('height')
    kwargs['color'] = flask.request.args.get('color')
    return flask.render_template('home.html', **kwargs)


@app.route('/index.html')
def home1():
    return app.send_static_file('index.html')


@app.route('/<user>/')
def echo(user):
    return flask.render_template('index.html', user=user)


@app.route('/get/')
def get_echo():
    kwargs = {}
    kwargs['user'] = flask.request.args.get('user')
    kwargs['lang'] = flask.request.args.get('lang')
    return flask.render_template('index2.html', **kwargs)

app.run(port=5000, debug=True)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import time
import webbrowser
import requests

time.sleep(5)

url = 'http://localhost:5000'

pages = ['index.html', 'kamimura', 'kamimura/',
         'get?user=kamimura&lang=python',
         '?color=red&height=10&thing=Gamera',
         'home.html?color=red&height=10&thing=Gamera',
         'home.html', '']

result = ''
for page in pages:
    full = '{0}/{1}'.format(url, page)
    webbrowser.open_new_tab(full)
    res = requests.get(full)
    result += res.text

time.sleep(5)
print(result)

入出力結果(Terminal, IPython)

$ ./client.py & ./app.py
[1] 7404
 * 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 - - [23/Aug/2016 00:15:30] "GET /index.html HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2016 00:15:31] "GET /kamimura HTTP/1.1" 301 -
127.0.0.1 - - [23/Aug/2016 00:15:31] "GET /kamimura/ HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2016 00:15:31] "GET /kamimura/ HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2016 00:15:32] "GET /get?user=kamimura&lang=python HTTP/1.1" 301 -
127.0.0.1 - - [23/Aug/2016 00:15:33] "GET /get/?user=kamimura&lang=python HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2016 00:15:33] "GET /kamimura/ HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2016 00:15:34] "GET /?color=red&height=10&thing=Gamera HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2016 00:15:35] "GET /get?user=kamimura&lang=python HTTP/1.1" 301 -
127.0.0.1 - - [23/Aug/2016 00:15:35] "GET /get/?user=kamimura&lang=python HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2016 00:15:36] "GET /home.html?color=red&height=10&thing=Gamera HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2016 00:15:36] "GET /?color=red&height=10&thing=Gamera HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2016 00:15:36] "GET /kamimura HTTP/1.1" 301 -
127.0.0.1 - - [23/Aug/2016 00:15:36] "GET /kamimura/ HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2016 00:15:37] "GET /home.html HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2016 00:15:37] "GET /home.html?color=red&height=10&thing=Gamera HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2016 00:15:38] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2016 00:15:39] "GET /home.html HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2016 00:15:39] "GET / HTTP/1.1" 200 -
<!doctype html>
<html>
  <head>
    <title>Flask Example</title>
  </head>
  <body>
    <h1>It's alive.</h1>
  </body>
</html>
<!doctype html>
<html>
  <head>
    <title>Flask Example</title>
  </head>
  <body>
    <h1>Hello, kamimura!</h1>
  </body>
</html><!doctype html>
<html>
  <head>
    <title>Flask Example</title>
  </head>
  <body>
    <h1>Hello, kamimura!</h1>
  </body>
</html><!doctype html>
<html>
  <head>
    <title>Flask Example</title>
  </head>
  <body>
    <h1>kamimura</h1>
    <h2>python</h2>
  </body>
</html><!doctype html>
<html>
  <head>
    <title>It's alive!</title>
  </head>
  <body>
    I'm of course referring to Gamera, which is 10 feet tall and
    red.
  </body>
</html><!doctype html>
<html>
  <head>
    <title>It's alive!</title>
  </head>
  <body>
    I'm of course referring to Gamera, which is 10 feet tall and
    red.
  </body>
</html><!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><!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>
^C[1]+  Done                    ./client.py
$

0 コメント:

コメントを投稿