代码之家  ›  专栏  ›  技术社区  ›  BCS

非regex WSGI调度程序

  •  2
  • BCS  · 技术社区  · 14 年前

    我找到这个了 regex based dispatcher 但我更愿意使用只使用文字前缀字符串的东西。这样的事情存在吗?

    2 回复  |  直到 14 年前
        1
  •  3
  •   unmounted    14 年前

    Flask / Werkzeug 有一个非凡的wsgiurl调度程序,它不是基于regex的。例如在烧瓶中:

    @myapp.route('/products/<category>/<item>')
    def product_page(category, item):
        pseudo_sql = select details from category where product_name = item;
        return render_template('product_page.html',\
                          product_details = formatted_db_output)
    

    这让你如愿以偿。, http://example.com/products/gucci/handbag 它是一个非常好的API。如果你只需要文字,它很简单:

    @myapp.route('/blog/searchtool')
    def search_interface():
        return some_prestored_string
    

    更新:

    from werkzeug.routing import Map, Rule
    
    url_map = Map([
        Rule('/', endpoint='index'),
        Rule('/<everything_else>/', endpoint='xedni'),
    ])
    
    def application(environ, start_response):
        urls = url_map.bind_to_environ(environ)
        endpoint, args = urls.match()
        start_response('200 OK', [('Content-Type', 'text/plain')])
        if endpoint == 'index':
            return 'welcome to reverse-a-path'
        else:
            backwards = environ['PATH_INFO'][::-1]
            return backwards
    

    你可以用Tornado、mod wsgi等来部署它。当然,很难击败烧瓶和瓶子的好习惯用法,或者Werkzeug的彻底性和质量 Map Rule

        2
  •  2
  •   Muhammad Alkarouri    14 年前

    不完全是你所描述的,但是你的需要可以通过使用 bottle . 这个 route decorator更有条理。瓶子不托管WSGI应用程序,尽管它可以作为WSGI应用程序托管。

    例子:

    from bottle import route, run
    
    @route('/:name')
    def index(name='World'):
        return '<b>Hello %s!</b>' % name
    
    run(host='localhost', port=8080)
    
        3
  •  0
  •   samwyse    6 年前

    class dispatcher(dict):
        def __call__(self, environ, start_response):
            key = wsgiref.util.shift_path_info(environ)
            try:
                value = self[key]
            except:
                send_error(404)
            try:
                value(environ, start_response)
            except:
                send_error(500)
    

    笔记

    1. 我们利用内置的'dict'类来获得很多功能。
    2. 您需要提供send_error例程。