在上一篇日志中已經討論和實現了根據url執行相應應用,在我閱讀了bottle.py官方文檔后,按照bottle的設計重寫一遍,主要借鑒大牛們的設計思想。
一個bottle.py的簡單實例
來看看bottle是如何使用的,代碼來自http://www.bottlepy.org/docs/0.12/index.html:
from bottle import route, run, template @route('/hello/<name>') def index(name): return template('<b>Hello {{name}}</b>!', name=name) run(host='localhost', port=8080)
很顯然,bottle是使用裝飾器來路由的。根據bottle的設計,我來寫一個簡單的框架。
Python裝飾器
裝飾器,顧名思義就是包裝一個函數。在不改變函數的同時,動態的給函數增加功能。這里不在探討更多的細節。
大致的框架
根據WSGI的定義,一個WSGI應用必須要是可調用的。所以下面是一個WSGI應用的大致框架:
class WSGIapp(object): def __init__(self): pass def route(self,path=None): pass def __call__(self,environ,start_response): return self.wsgi(environ,start_response) def wsgi(self,environ,start_response): pass
其中,route方法就是來保存url->target的。這里為了方便,將url->target保存在字典中:
def route(self,path=None): def decorator(func): self.routes[path] = func return func return decorator
這里return func注釋掉也可以,求大神解釋一下啊!!
然后就是實現WSGIapp的每個方法:
class WSGIapp(object): def __init__(self): self.routes = {} def route(self,path=None): def decorator(func): self.routes[path] = func return func return decorator def __call__(self,environ,start_response): print 'call' return self.wsgi(environ,start_response) def wsgi(self,environ,start_response): path = environ['PATH_INFO'] print path if path in self.routes: status = '200 OK' response_headers = [('Content-Type','text/plain')] start_response(status,response_headers) print self.routes[path]() return self.routes[path]() else: status = '404 Not Found' response_headers = [('Content-Type','text/plain')] start_response(status,response_headers) return '404 Not Found!' app = WSGIapp() @app.route('/') def index(): return ['This is index'] @app.route('/hello') def hello(): return ['hello'] from wsgiref.simple_server import make_server httpd = make_server('',8000,app) print 'start....' httpd.serve_forever()
這樣,一個簡易的web框架的雛形就實現了,如果對裝飾器中的路徑加入正則表達式,那么就可以很輕松的應對URL了。下一篇日志就是加入模板引擎jinja2了。