python中WSGI是什麼,Python應用WSGI詳解,pythonwsgi

來源:互聯網
上載者:User

python中WSGI是什麼,Python應用WSGI詳解,pythonwsgi

為了讓大家更好的對python中WSGI有更好的理解,我們先從最簡單的認識WSGI著手,然後介紹一下WSGI幾個經常使用到的介面,瞭解基本的用法和功能,最後,我們通過執行個體瞭解一下WSGI在實際項目中如何使用。

WSGI是什嗎?

wsgi是一個web組件的介面防範,wsgi將web組件分為三類:web伺服器,web中介軟體,web應用程式

wsgi基本處理模式為:wsgi Server -> wsgi middleware -> wsgi application

WSGI,全稱 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是為 Python 語言定義的 Web 服務器和 Web 應用程式或架構之間的一種簡單而通用的介面。自從 WSGI 被開發出來以後,許多其它語言中也出現了類似介面。

WSGI 的官方定義是,the Python Web Server Gateway Interface。從名字就可以看出來,這東西是一個Gateway,也就是網關。網關的作用就是在協議之間進行轉換。

WSGI 是作為 Web 服務器與 Web 應用程式或應用程式框架之間的一種低層級的介面,以提升可移植 Web 應用程式開發的共同點。WSGI 是基於現存的 CGI 標準而設計的。

很多架構都內建了 WSGI server ,比如 Flask,webpy,Django、CherryPy等等。當然效能都不好,內建的 web server 更多的是測試用途,發布時則使用生產環境的 WSGI server或者是聯合 nginx 做 uwsgi 。

也就是說,WSGI就像是一座橋樑,一邊連著web伺服器,另一邊連著使用者的應用。但是呢,這個橋的功能很弱,有時候還需要別的橋來幫忙才能進行處理。

WSGI的作用

WSGI有兩方:“伺服器”或“網關”一方,以及“應用程式”或“應用程式框架”一方。服務方調用應用方,提供環境資訊,以及一個回呼函數(提供給應用程式用來將訊息頭傳遞給伺服器方),並接收Web內容作為傳回值。

所謂的 WSGI中介軟體同時實現了API的兩方,因此可以在WSGI服務和WSGI應用之間起調解作用:從WSGI伺服器的角度來說,中介軟體扮演應用程式,而從應用程式的角度來說,中介軟體扮演伺服器。“中介軟體”組件可以執行以下功能:

重寫環境變數後,根據目標URL,將請求訊息路由到不同的應用對象。

允許在一個進程中同時運行多個應用程式或應用程式框架。

負載平衡和遠端,通過在網路上轉寄請求和響應訊息。

進行內容後處理,例如應用XSLT樣式表。

wsgi server:

理解為一個符合wsgi規範的web server,接收request請求,封裝一系列環境變數,按照wsgi規範調用註冊的wsgi app,最後將response返回給用戶端。

工作流程:

1、伺服器建立socket,監聽port,等待client 串連

2、當請求過來時,server解析client msg放到環境變數environ中,並調用綁定的handler來處理

3、handler解析這個http請求,將請求訊息例如method、path等放到environ中

4、wsgi handler再將一些server端訊息也放到environ中,最後server msg,client msg,以及本次請求msg 全部都儲存到了環境變數envrion中;

5、wsgi handler調用註冊的wsgi app,並將envrion和回呼函數傳給wsgi app

6、wsgi app將reponse header/status/body回傳給wsgi handler

7、handler 通過socket將response msg返回到client

WSGI Application

wsgi application就是一個普通的callable對象,當有請求到來時,wsgi server會調用這個wsgi app。這個對象接收兩個參數,通常為environ,start_response。environ就像前面介紹的,可以理解為環境變數,

跟一次請求相關的所有資訊都儲存在了這個環境變數中,包括伺服器資訊,用戶端資訊,請求資訊。start_response是一個callback函數,wsgi application通過調用start_response,將response headers/status 返回給wsgi server。此外這個wsgi app會return 一個iterator對象 ,這個iterator就是response body。

Dispatcher Middleware,用來實現URL 路由:(代碼說明)

#!/usr/bin/python #encoding=utf-8#利用wsgiref 作為wsgi serverfrom wsgiref.simple_server import make_server"""def simple_app(environ, start_response):status = '200 ok'response_headers = [('Content-type', 'text/plain')] #設定http頭start_response(status, response_headers)return [u"test wsgi app".encode('utf-8')]class AppClass(object):def __call__(self, environ, start_response):status = "200 ok"response_headers = [('Content-type', 'text/plain')]start_response(status, response_headers)return [u"class AppClass".encode('utf-8')]"""#wsgi app只要是一個callable對象即可,不一定要是函數#一個實現了__call__方法樣本也ok的#httpd = make_server('', 8080, simple_app)"""app = AppClass()httpd = make_server('', 8080, app)httpd.serve_forever()"""URL_PATTERNS = (('AA/', 'AA_app'),('BB/', 'BB_app'),)class Dispatcher(object):#實現路由功能:def _match(self, path):path = path.split('/')[1]for url, app in URL_PATTERNS:if path in url:return appdef __call__(self, environ, start_response):path = environ.get('PATH_INFO', '/')app = self._match(path)if app:app = globals()[app]return app(environ, start_response)else:start_response("404 NOT FOUND",[('Content-type', 'text/plain')])return ["page dose not exists"]def AA_app(environ, start_response):start_response("200 OK",[('Content-type', 'text/html')])return ["AA page"]def BB_app(environ, start_response):start_response("200 OK",[('Content-type', 'text/html')]) return ["BB page"]app = Dispatcher()httpd = make_server('', 8090, app)httpd.serve_forever()測試結果:server端:root@u163:~/cp163/python# python wsgi_app.py 192.168.2.162 - - [04/Nov/2015 18:44:06] "GET /AA HTTP/1.1" 200 7192.168.2.162 - - [04/Nov/2015 18:44:22] "GET /BB HTTP/1.1" 200 7client端:root@u162:~# curl http://192.168.2.163:8090/AAAA pageroot@u162:~# curl http://192.168.2.163:8090/BBBB pageroot@u162:~#

下面在給大家推薦一篇關機介面的詳細介紹文章:深入解析Python中的WSGI介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.