WSGI Webob Routes 实例

本文地址:http://blog.csdn.net/spch2008/article/details/9004926


1.下载库文件

   webob库:http://download.csdn.net/detail/spch2008/5497755

   routes库:http://download.csdn.net/detail/spch2008/5497757

   repoze库:http://download.csdn.net/detail/spch2008/5499231

2. 组织代码

    WSGI Webob Routes 实例_第1张图片

3. 代码

   

'''
Created on 2013-6-1

@author: spch2008
'''

from wsgiref.simple_server import make_server

import routes.middleware
import webob.dec
import webob.exc

class Controller:
    @webob.dec.wsgify
    def __call__(self, req):
        return webob.Response("Hello World!")


 
class Router(object):
    def __init__(self):
        self._mapper = routes.Mapper()
        self._mapper.connect('/spch',  
                        controller=Controller(),  
                        action='index',  
                        conditions={'method': ['GET']})  
        
        self._router = routes.middleware.RoutesMiddleware(self._dispatch, self._mapper)

    @webob.dec.wsgify
    def __call__(self, req):
        
        return self._router

    @staticmethod
    @webob.dec.wsgify
    def _dispatch(req):
        match = req.environ['wsgiorg.routing_args'][1]
                
        if not match:
            return webob.exc.HTTPNotFound()
        
        app = match['controller']  
        return app
        
      

app = Router()
httpd = make_server('localhost', 8282, app)  
httpd.serve_forever() 


  22行:创建一个mapper

  23行:#注册一个路由

  28行:创建一个RoutesMiddleware对象,匹配路由,修改环境变量后,调用self._dispatch


4. 运行结果

    WSGI Webob Routes 实例_第2张图片

你可能感兴趣的:(WSGI Webob Routes 实例)