国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

深入解析Python中的WSGI接口

2019-11-25 17:29:43
字體:
來源:轉載
供稿:網友

概述

WSGI接口包含兩方面:server/gateway 及 application/framework。
server調用由application提供的可調用對象。
另外在server和application之間還可能有一種稱作middleware的中間件。
可調用對象是指:函數、方法、類或者帶有callable方法的實例。
關于application

函數、方法、類及帶有callable方法的實例等可調用對象都可以作為the application object。
WSGI協議要求:
the application object接受兩個參數且可以被多次調用

這兩個參數分別為:
1.CGI式的字典;
2.回調函數:application用來向server傳遞http狀態碼/消息/http頭

另外協議要求可調用對象必須將響應體封裝成一個可迭代的strings返回。

# the application object. 可以使用其他名字,# 但是在使用mod_wsgi 時必須為 "application"def application( environ, start_response):# 函數接受兩個參數:# environ :包含有CGI 式環境變量的字典,由server負責提供內容# start_response:由server提供的回調函數,其作用是將狀態碼和響應頭返回給server# 構造響應體,以可迭代字符串形式封裝  response_body = 'The request method was %s' % environ['REQUEST_METHOD']# HTTP 響應碼及消息  status = '200 OK'# 提供給客戶端的響應頭.# 封裝成list of tuple pairs 的形式:# 格式要求:[(Header name, Header value)].  response_headers = [('Content-Type', 'text/plain'),            ('Content-Length', str(len(response_body)))]# 將響應碼/消息及響應頭通過傳入的start_reponse回調函數返回給server  start_response(status, response_headers)# 響應體作為返回值返回# 注意這里被封裝到了list中.  return [response_body]

關于server

從概述中可以知道,WSGI server必須要調用application,同時,從application的協議要求可知:
1. WSGI server必須向application提供環境參數,因此,自身也必須能夠獲取環境參數。
2. WSGI server接收application的返回值作為響應體。
最簡單的WSGI server為Python自帶的wsgiref.simple_server
示例如下:

from wsgiref.simple_server import make_serversrv = make_server('localhost', 8080, hello_world)srv.serve_forever()

關于middleware

middleware的概念沒有appllication和server那么容易理解。
假設一個符合application標準的可調用對象,它接受可調用對象作為參數,返回一個可調用對象的對象。
那么對于server來說,它是一個符合標準的可調用對象,因此是application。
而對于application來說,它可以調用application,因此是server。
這樣的可調用對象稱為middleware。

middleware的概念非常接近decorator。

以一個路由的例子示例:

import re# 這是一個標準的application objectdef index(environ, start_response):  start_response('200 OK', [('Content-Type', 'text/html')])  return ['index page']# 這是一個標準的application objectdef hello(environ, start_response):  start_response('200 OK', [('Content-Type', 'text/html')])  return ['hello page']# 這是一個標準的application objectdef not_found(environ, start_response):  start_response('404 NOT FOUND', [('Content-Type', 'text/plain')])  return ['Not Found Page']# map urls to functionsurls = [  (r'^$', index),  (r'hello/?$', hello)]# 這是一個middleware# 根據不同的route返回不同的application objectdef application(environ, start_response):  path = environ.get('PATH_INFO', '').lstrip('/')  for regex, callback in urls:    match = re.search(regex, path)    if match is not None:

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 敖汉旗| 正安县| 年辖:市辖区| 丹棱县| 保亭| 东平县| 大方县| 宁波市| 陆河县| 海南省| 北流市| 厦门市| 成安县| 墨竹工卡县| 塔城市| 宜章县| 建德市| 获嘉县| 堆龙德庆县| 潍坊市| 那曲县| 原平市| 新乡县| 樟树市| 双牌县| 博乐市| 冀州市| 邮箱| 清涧县| 拉萨市| 丹阳市| 南康市| 双江| 改则县| 新蔡县| 谢通门县| 赞皇县| 宁津县| 苏州市| 新巴尔虎左旗| 马龙县|