JY-CONTENTS

JY

JY-CONTENTS
search

+

MENU

【Python】webdispatch

【Python】webdispatch

(DATE)

-

2017.08.26

(CATEGORY)

-

webdispatch.URLDispatcher

URLDispatcher は URL パターンで処理を振り分けるライブラリです。

URL で処理を振り分ける

アクセスする URL によって実行する wsgi アプリケーションを振り分けます。

from webob.dec import wsgify
 
@wsgify
def hello(request):
    return 'Hello'
 
@wsgify
def bye(request):
    return 'Bye'
 
 
#url での振り分け
from webdispatch import URLDispatcher
app = URLDispatcher()
 
#第1引数:パターン名 第2引数:パス 第3引数:実行する関数
#http://localhost:8080/hh でアクセスしたときは hello アプリを実行
app.add_url('hh', '/hh', hello)
 
#http://localhost:8080/bb でアクセスしたときは bye アプリを実行
app.add_url('bb', '/bb', bye)
 
 
from wsgiref.simple_server import make_server
server = make_server('', 8080, app)
server.serve_forever()

test.py を実行後

Hello

Bye

Not found

プレースホルダ

URL パターンでプレースフォルダを使う事ができます。

from webob.dec import wsgify
from webdispatch import URLDispatcher
 
 
@wsgify
def hello(request):
    #第1引数に URL で指定された名前
    #第2引数には URL で名前が指定されなかったときのデフォルト
    name = request.urlvars.get('name', 'world')
    return "Hello, {name}!".format(name=name)
 
 
 
app = URLDispatcher()
#http://localhost:8080/hh でアクセス。名前は指定していないので「world」が適用。
app.add_url('hh', '/hh', hello)
 
 
#http://localhost:8080/hh/taro でアクセス。
#{name} に「taro」が適用。
app.add_url('hh_name', '/hh/{name}', hello)
 
 
from wsgiref.simple_server import make_server
server = make_server('', 8080, app)
server.serve_forever()

test.py を実行後

URL を生成する

URLDispatcher は environ[‘webdispatch.urlgenerator’] に URL のジェネレータを作成します。
作成された URL のジェネレータの generate メソッドは、add_url で登録された URL パターンに値を埋め込んで URL を生成します。

from webob.dec import wsgify
from webdispatch import URLDispatcher
 
 
@wsgify
def hello(request):
    #environ['webdispatch.urlgenerator'] に URL のジェネレータを作成
    gen = request.environ['webdispatch.urlgenerator']
    #generate で add_url で登録された URL パターン(hh_val)に値(python)を埋め込んで URL を生成し返す
    return gen.generate('hh_val', val='python')
 
 
app = URLDispatcher()
app.add_url('hh', '/hh', hello)
app.add_url('hh_val', '/hh/{val}', hello)
 
 
from wsgiref.simple_server import make_server
server = make_server('', 8080, app)
server.serve_forever()

test.py を実行後

webdispatch.MethodDispatcher

HTTP リクエストで処理を振り分けます。
下記は、get メソッドの場合はフォームを表示し、post メソッドの場合はフォームからの内容を処理します。

from webob.dec import wsgify
from webdispatch import MethodDispatcher
 
 
#フォームを表示する wsgi アプリ
@wsgify
def hello_form(request):
    return """\
<form method="post">
<input name="name">
<button type="submit">POST</button>
</form>
"""
 
#フォームからの値を処理し表示する wsgi アプリ
@wsgify
def hello(request):
    #フォームの「name」の値を取得
    name = request.params.get('name', 'world')
    return "Hello, {name}!".format(name=name)
 
 
app = MethodDispatcher()
#get メソッドの場合の処理
app.register_app('get', hello_form)
#post メソッドの場合の処理
app.register_app('post',  hello)
 
 
from wsgiref.simple_server import make_server
server = make_server('', 8080, app)
server.serve_forever()

test.py を実行後

が表示される。
「taro」と入力して post データを送信する。

「Hello, taro!」とブラウザに表示されます。

NEW TOPICS

/ ニュー & アップデート

SEE ALSO

/ 似た記事を見る

JY CONTENTS UNIQUE BLOG

search-menu search-menu