深入解析Python中的WSGI介面

來源:互聯網
上載者:User

深入解析Python中的WSGI介面

   這篇文章主要介紹了深入解析Python中的WSGI介面,WSGI介面是Python中網路架構串連伺服器的必備工具,需要的朋友可以參考下

  概述

  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返回。

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

# 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

  樣本如下:

  ?

1

2

3

from wsgiref.simple_server import make_server

srv = make_server('localhost', 8080, hello_world)

srv.serve_forever()

  關於middleware

  middleware的概念沒有appllication和server那麼容易理解。

  假設一個符合application標準的可調用對象,它接受可調用對象作為參數,返回一個可調用對象的對象。

  那麼對於server來說,它是一個符合標準的可調用對象,因此是application。

  而對於application來說,它可以調用application,因此是server。

  這樣的可調用對象稱為middleware。

  middleware的概念非常接近decorator。

  以一個路由的例子樣本:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

import re

 

# 這是一個標準的application object

def index(environ, start_response):

start_response('200 OK', [('Content-Type', 'text/html')])

return ['index page']

 

# 這是一個標準的application object

def hello(environ, start_response):

start_response('200 OK', [('Content-Type', 'text/html')])

return ['hello page']

 

# 這是一個標準的application object

def not_found(environ, start_response):

start_response('404 NOT FOUND', [('Content-Type', 'text/plain')])

return ['Not Found Page']

 

# map urls to functions

urls = [

(r'^$', index),

(r'hello/?$', hello)

]

# 這是一個middleware

# 根據不同的route返回不同的application object

def 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:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.