WSGI和PASTE

WSGI就是一個標準,WSGI server就是實現了這個標準的一個容器。這個標準相似於以下的東東:python

1
2
3
4
5
6
7
8
9
10
11
from wsgiref.simple_server import make_server
 
def simple_app(environ, start_response):
     status = '200 OK'
     response_headers = [( 'Content-type' , 'text/plain' )]
     start_response(status, response_headers)
     return [u "This is hello wsgi app" .encode( 'utf8' )]
 
httpd = make_server('', 8000 , simple_app)
print "Serving on port 8000..."
httpd.serve_forever()

web應用開發者只要聽從WSGI的標準,編寫simple_app就能夠實現本身的應用了。標準很簡單:第一個environ參數代表了全部 request相關的環境變量,第二個start_response用於寫入一些response的返回頭的信息,而後再return返回的 response的數據就好了。這個就是全部的WSGI標準了。web

而WSGI的server其實作的事情也很簡單,能夠參考http://blog.csdn.net/sraing/article/details/8455242。json

至於paste則是用於配置WSGI的URL和對應APP的工具,具體的能夠參考http://blog.csdn.net/sonicatnoc /article/details/6539716。簡單的說就是經過paste,我能把/index映射到XXX.py的 handle_index(status, response_headers)上去。api

paste的配置文件中有下面幾項是比較常見的:app

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
filter:
如:
[filter:s3_extension]
paste.filter_factory = keystone.contrib.s3:S3Extension.factory
 
app:
如:
[app:service_v3]
paste.app_factory = keystone.service:v3_app_factory
 
pipeline:
如:
[pipeline:public_api]
pipeline = sizelimit url_normalize build_auth_context token_auth admin_token_auth xml_body json_body ec2_extension user_crud_extension public_service
 
composite:
如:
[composite:main]
use = egg:Paste#urlmap
/v2.0 = public_api
/v3 = api_v3
/ = public_version_api

其中:
composite中註冊對應的URL,pipline則指明一串app的傳遞鏈,這些傳遞鏈中的對象能夠分別在filter和app中找 到,filter和app都表明着某個module中的一個callable對象,這些callable能夠在對應的module中找到對應的 function,app是一個callable object,接受的參數(environ,start_response),這是paste系統交給application的,符合WSGI規範的參 數. app須要完成的任務是響應envrion中的請求,準備好響應頭和消息體,而後交給start_response處理,並返回響應消息體。filter 是一個callable object,其惟一參數是(app),這是WSGI的application對象,filter須要完成的工做是將application包裝成另外一個 application(「過濾」),並返回這個包裝後的application。app這個callable須要由app_factory得 到,app_factory是一個callable object,其接受的參數是一些關於application的配置信息:(global_conf,**kwargs),global_conf是在 ini文件中default section中定義的一系列key-value對,而**kwargs,即一些本地配置,是在ini文件中,app:xxx section中定義的一系列key-value對。app_factory返回值是一個application對象。filter這個callable 須要由filter_factory獲得,filter_factory是一個callable object,其接受的參數是一系列關於filter的配置信息:(global_conf,**kwargs),global_conf是在ini文件 中default section中定義的一系列key-value對,而**kwargs,即一些本地配置,是在ini文件中,filter:xxx section中定義的一系列key-value對。filter_factory返回一個filter對象。工具

相關文章
相關標籤/搜索