wsgiref --- WSGI 工具和參考實(shí)現?


Web 服務(wù)器網(wǎng)關(guān)接口(WSGI)是 Web 服務(wù)器軟件和用 Python 編寫(xiě)的 Web 應用程序之間的標準接口。 具有標準接口能夠讓支持 WSGI 的應用程序與多種不同的 Web 服務(wù)器配合使用。

只有 Web 服務(wù)器和編程框架的開(kāi)發(fā)者才需要了解 WSGI 設計的每個(gè)細節和邊界情況。 你不需要了解 WSGI 的每個(gè)細節而只需要安裝一個(gè) WSGI 應用程序或編寫(xiě)使用現有框架的 Web 應用程序。

wsgiref is a reference implementation of the WSGI specification that can be used to add WSGI support to a web server or framework. It provides utilities for manipulating WSGI environment variables and response headers, base classes for implementing WSGI servers, a demo HTTP server that serves WSGI applications, types for static type checking, and a validation tool that checks WSGI servers and applications for conformance to the WSGI specification (PEP 3333).

參看 wsgi.readthedocs.io 獲取有關(guān) WSGI 的更多信息,以及教程和其他資源的鏈接。

wsgiref.util -- WSGI 環(huán)境工具?

This module provides a variety of utility functions for working with WSGI environments. A WSGI environment is a dictionary containing HTTP request variables as described in PEP 3333. All of the functions taking an environ parameter expect a WSGI-compliant dictionary to be supplied; please see PEP 3333 for a detailed specification and WSGIEnvironment for a type alias that can be used in type annotations.

wsgiref.util.guess_scheme(environ)?

返回對于 wsgi.url_scheme 應為 "http" 還是 "https" 的猜測,具體方式是在 environ 中檢查 HTTPS 環(huán)境變量。 返回值是一個(gè)字符串。

此函數適用于創(chuàng )建一個(gè)包裝了 CGI 或 CGI 類(lèi)協(xié)議例如 FastCGI 的網(wǎng)關(guān)。 通常,提供這種協(xié)議的服務(wù)器將包括一個(gè) HTTPS 變量并會(huì )在通過(guò) SSL 接收請求時(shí)將其值設為 "1", "yes" 或 "on"。 這樣,此函數會(huì )在找到上述值時(shí)返回 "https",否則返回 "http"。

wsgiref.util.request_uri(environ, include_query=True)?

使用 PEP 3333 的 "URL Reconstruction" 一節中的算法返回完整的請求 URL,可能包括查詢(xún)字符串。 如果 include_query 為假值,則結果 URI 中將不包括查詢(xún)字符串。

wsgiref.util.application_uri(environ)?

類(lèi)似于 request_uri(),區別在于 PATH_INFOQUERY_STRING 變量會(huì )被忽略。 結果為請求所指定的應用程序對象的基準 URI。

wsgiref.util.shift_path_info(environ)?

將單個(gè)名稱(chēng)從 PATH_INFO 變換為 SCRIPT_NAME 并返回該名稱(chēng)。 environ 目錄將被原地 修改;如果你需要保留原始 PATH_INFOSCRIPT_NAME 不變請使用一個(gè)副本。

如果 PATH_INFO 中沒(méi)有剩余的路徑節,則返回 None。

通常,此例程被用來(lái)處理請求 URI 路徑的每個(gè)部分,比如說(shuō)將路徑當作是一系列字典鍵。 此例程會(huì )修改傳入的環(huán)境以使其適合發(fā)起調用位于目標 URI 上的其他 WSGI 應用程序,如果有一個(gè) WSGI 應用程序位于 /foo,而請求 URI 路徑為 /foo/bar/baz,且位于 /foo 的 WSGI 應用程序調用了 shift_path_info(),它將獲得字符串 "bar",而環(huán)境將被更新以適合傳遞給位于 /foo/bar 的 WSGI 應用程序。 也就是說(shuō),SCRIPT_NAME 將由 /foo 變?yōu)?/foo/bar,而 PATH_INFO 將由 /bar/baz 變?yōu)?/baz。

PATH_INFO 只是 "/" 時(shí),此例程會(huì )返回一個(gè)空字符串并在 SCRIPT_NAME 末尾添加一個(gè)斜杠,雖然空的路徑節通常會(huì )被忽略,并且 SCRIPT_NAME 通常也不以斜杠作為結束。 此行為是有意為之的,用來(lái)確保應用程序在使用此例程執行對象遍歷時(shí)能區分以 /x 結束的和以 /x/ 結束的 URI。

wsgiref.util.setup_testing_defaults(environ)?

以簡(jiǎn)短的默認值更新 environ 用于測試目的。

此例程會(huì )添加 WSGI 所需要的各種參數,包括 HTTP_HOST, SERVER_NAME, SERVER_PORT, REQUEST_METHOD, SCRIPT_NAME, PATH_INFO 以及 PEP 3333 中定義的所有 wsgi.* 變量。 它只提供默認值,而不會(huì )替換這些變量的現有設置。

此例程的目的是讓 WSGI 服務(wù)器的單元測試以及應用程序設置測試環(huán)境更為容易。 它不應該被實(shí)際的 WSGI 服務(wù)器或應用程序所使用,因為它用的是假數據!

用法示例:

from wsgiref.util import setup_testing_defaults
from wsgiref.simple_server import make_server

# A relatively simple WSGI application. It's going to print out the
# environment dictionary after being updated by setup_testing_defaults
def simple_app(environ, start_response):
    setup_testing_defaults(environ)

    status = '200 OK'
    headers = [('Content-type', 'text/plain; charset=utf-8')]

    start_response(status, headers)

    ret = [("%s: %s\n" % (key, value)).encode("utf-8")
           for key, value in environ.items()]
    return ret

with make_server('', 8000, simple_app) as httpd:
    print("Serving on port 8000...")
    httpd.serve_forever()

除了上述的環(huán)境函數,wsgiref.util 模塊還提供了以下輔助工具:

wsgiref.util.is_hop_by_hop(header_name)?

如果 'header_name' 是 RFC 2616 所定義的 HTTP/1.1 "Hop-by-Hop" 標頭則返回 True。

class wsgiref.util.FileWrapper(filelike, blksize=8192)?

A concrete implementation of the wsgiref.types.FileWrapper protocol used to convert a file-like object to an iterator. The resulting objects are iterables. As the object is iterated over, the optional blksize parameter will be repeatedly passed to the filelike object's read() method to obtain bytestrings to yield. When read() returns an empty bytestring, iteration is ended and is not resumable.

如果 filelike 具有 close() 方法,返回的對象也將具有 close() 方法,并且它將在被調用時(shí)發(fā)起調用 filelike 對象的 close() 方法。

用法示例:

from io import StringIO
from wsgiref.util import FileWrapper

# We're using a StringIO-buffer for as the file-like object
filelike = StringIO("This is an example file-like object"*10)
wrapper = FileWrapper(filelike, blksize=5)

for chunk in wrapper:
    print(chunk)

在 3.11 版更改: Support for __getitem__() method has been removed.

wsgiref.headers -- WSGI 響應標頭工具?

此模塊提供了一個(gè)單獨的類(lèi) Headers,可以方便地使用一個(gè)映射類(lèi)接口操作 WSGI 響應標頭。

class wsgiref.headers.Headers([headers])?

創(chuàng )建一個(gè)包裝了 headers 的映射類(lèi)對象,它必須為如 PEP 3333 所描述的由標頭名稱(chēng)/值元組構成的列表。 headers 的默認值為一個(gè)空列表。

Headers 對象支持典型的映射操作包括 __getitem__(), get(), __setitem__(), setdefault(), __delitem__()__contains__()。 對于以上各個(gè)方法,映射的鍵是標頭名稱(chēng)(大小寫(xiě)不敏感),而值是關(guān)聯(lián)到該標頭名稱(chēng)的第一個(gè)值。 設置一個(gè)標頭會(huì )移除該標頭的任何現有值,再將一個(gè)新值添加到所包裝的標頭列表末尾。 標頭的現有順序通常會(huì )保持不變,只在所包裝的列表末尾添加新的標頭。

與字典不同,當你試圖獲取或刪除所包裝的標頭列表中不存在的鍵時(shí) Headers 對象不會(huì )引發(fā)錯誤。 獲取一個(gè)不存在的標頭只是返回 None,而刪除一個(gè)不存在的標頭則沒(méi)有任何影響。

Headers 對象還支持 keys(), values() 以及 items() 方法。 如果存在具有多個(gè)值的鍵則 keys()items() 所返回的列表可能包括多個(gè)相同的鍵。 對 Headers 對象執行 len() 的結果與 items() 的長(cháng)度相同,也與所包裝的標頭列表的長(cháng)度相同。 實(shí)際上,items() 方法只是返回所包裝的標頭列表的一個(gè)副本。

Headers 對象上調用 bytes() 將返回適用于作為 HTTP 響應標頭來(lái)傳輸的已格式化字節串。 每個(gè)標頭附帶以逗號加空格分隔的值放置在一行中。 每一行都以回車(chē)符加換行符結束,且該字節串會(huì )以一個(gè)空行作為結束。

除了映射接口和格式化特性,Headers 對象還具有下列方法用來(lái)查詢(xún)和添加多值標頭,以及添加具有 MIME 參數的標頭:

get_all(name)?

返回包含指定標頭的所有值的列表。

返回的列表項將按它們在原始標頭列表中的出現或被添加到實(shí)例中的順序排序,并可能包含重復項。 任何被刪除并重新插入的字段總是會(huì )被添加到標頭列表末尾。 如果給定名稱(chēng)的字段不存在,則返回一個(gè)空列表。

add_header(name, value, **_params)?

添加一個(gè)(可能有多個(gè)值)標頭,帶有通過(guò)關(guān)鍵字參數指明的可選的 MIME 參數。

name 是要添加的標頭字段。 可以使用關(guān)鍵字參數來(lái)為標頭字段設置 MIME 參數。 每個(gè)參數必須為字符串或 None。 參數名中的下劃線(xiàn)會(huì )被轉換為連字符,因為連字符不可在 Python 標識符中出現,但許多 MIME 參數名都包括連字符。 如果參數值為字符串,它會(huì )以 name="value" 的形式被添加到標頭值參數中。 如果為 None,則只會(huì )添加參數名。 (這適用于沒(méi)有值的 MIME 參數。) 示例用法:

h.add_header('content-disposition', 'attachment', filename='bud.gif')

以上代碼將添加一個(gè)這樣的標頭:

Content-Disposition: attachment; filename="bud.gif"

在 3.5 版更改: headers 形參是可選的。

wsgiref.simple_server -- 一個(gè)簡(jiǎn)單的 WSGI HTTP 服務(wù)器?

此模塊實(shí)現了一個(gè)簡(jiǎn)單的 HTTP 服務(wù)器 (基于 http.server) 來(lái)發(fā)布 WSGI 應用程序。 每個(gè)服務(wù)器實(shí)例會(huì )在給定的主機名和端口號上發(fā)布一個(gè) WSGI 應用程序。 如果你想在一個(gè)主機名和端口號上發(fā)布多個(gè)應用程序,你應當創(chuàng )建一個(gè)通過(guò)解析 PATH_INFO 來(lái)選擇每個(gè)請求要發(fā)起調用哪個(gè)應用程序的 WSGI 應用程序。 (例如,使用 wsgiref.util 中的 shift_path_info() 函數。)

wsgiref.simple_server.make_server(host, port, app, server_class=WSGIServer, handler_class=WSGIRequestHandler)?

創(chuàng )建一個(gè)新的 WSGI 服務(wù)器并在 hostport 上進(jìn)行監聽(tīng),接受對 app 的連接。 返回值是所提供的 server_class 的實(shí)例,并將使用指定的 handler_class 來(lái)處理請求。 app 必須是一個(gè)如 PEP 3333 所定義的 WSGI 應用程序對象。

用法示例:

from wsgiref.simple_server import make_server, demo_app

with make_server('', 8000, demo_app) as httpd:
    print("Serving HTTP on port 8000...")

    # Respond to requests until process is killed
    httpd.serve_forever()

    # Alternative: serve one request, then exit
    httpd.handle_request()
wsgiref.simple_server.demo_app(environ, start_response)?

此函數是一個(gè)小巧但完整的 WSGI 應用程序,它返回一個(gè)包含消息 "Hello world!" 以及 environ 形參中提供的鍵/值對的文本頁(yè)面。 它適用于驗證 WSGI 服務(wù)器 (例如 wsgiref.simple_server) 是否能夠正確地運行一個(gè)簡(jiǎn)單的 WSGI 應用程序。

class wsgiref.simple_server.WSGIServer(server_address, RequestHandlerClass)?

創(chuàng )建一個(gè) WSGIServer 實(shí)例。 server_address 應當是一個(gè) (host,port) 元組,而 RequestHandlerClass 應當是 http.server.BaseHTTPRequestHandler 的子類(lèi),它將被用來(lái)處理請求。

你通常不需要調用此構造器,因為 make_server() 函數能為你處理所有的細節。

WSGIServerhttp.server.HTTPServer 的子類(lèi),因此它所有的方法 (例如 serve_forever()handle_request()) 都是可用的。 WSGIServer 還提供了以下 WSGI 專(zhuān)屬的方法:

set_app(application)?

將可調用對象 application 設為將要接受請求的 WSGI 應用程序。

get_app()?

返回當前設置的應用程序可調用對象。

但是,你通常不需要使用這些附加的方法,因為 set_app() 通常會(huì )由 make_server() 來(lái)調用,而 get_app() 主要是針對請求處理句柄實(shí)例的。

class wsgiref.simple_server.WSGIRequestHandler(request, client_address, server)?

為給定的 request 創(chuàng )建一個(gè) HTTP 處理句柄 (例如套接字),client_address (一個(gè) (host,port) 元組),以及 server (WSGIServer 實(shí)例)。

你不需要直接創(chuàng )建該類(lèi)的實(shí)例;它們會(huì )根據 WSGIServer 對象的需要自動(dòng)創(chuàng )建。 但是,你可以子類(lèi)化該類(lèi)并將其作為 handler_class 提供給 make_server() 函數。 一些可能在子類(lèi)中重載的相關(guān)方法:

get_environ()?

Return a WSGIEnvironment dictionary for a request. The default implementation copies the contents of the WSGIServer object's base_environ dictionary attribute and then adds various headers derived from the HTTP request. Each call to this method should return a new dictionary containing all of the relevant CGI environment variables as specified in PEP 3333.

get_stderr()?

返回應被用作 wsgi.errors 流的對象。 默認實(shí)現將只返回 sys.stderr。

handle()?

處理 HTTP 請求。 默認的實(shí)現會(huì )使用 wsgiref.handlers 類(lèi)創(chuàng )建一個(gè)處理句柄實(shí)例來(lái)實(shí)現實(shí)際的 WSGI 應用程序接口。

wsgiref.validate --- WSGI 一致性檢查器?

當創(chuàng )建新的 WSGI 應用程序對象、框架、服務(wù)器或中間件時(shí),使用 wsgiref.validate 來(lái)驗證新代碼的一致性是很有用的。 此模塊提供了一個(gè)創(chuàng )建 WSGI 應用程序對象的函數來(lái)驗證 WSGI 服務(wù)器或網(wǎng)關(guān)與 WSGI 應用程序對象之間的通信,以便檢查雙方的協(xié)議一致性。

請注意這個(gè)工具并不保證完全符合 PEP 3333;此模塊沒(méi)有報告錯誤并不一定表示不存在錯誤。 但是,如果此模塊確實(shí)報告了錯誤,那么幾乎可以肯定服務(wù)器或應用程序不是 100% 符合要求的。

此模塊是基于 Ian Bicking 的 "Python Paste" 庫的 paste.lint 模塊。

wsgiref.validate.validator(application)?

包裝 application 并返回一個(gè)新的 WSGI 應用程序對象。 返回的應用程序將轉發(fā)所有請求到原始的 application,并將檢查 application 和發(fā)起調用它的服務(wù)器是否都符合 WSGI 規范和 RFC 2616。

任何被檢測到的不一致性都會(huì )導致引發(fā) AssertionError;但是請注意,如何對待這些錯誤取決于具體服務(wù)器。 例如,wsgiref.simple_server 和其他基于 wsgiref.handlers 的服務(wù)器(它們沒(méi)有重載錯誤處理方法來(lái)做其他操作)將簡(jiǎn)單地輸出一條消息報告發(fā)生了錯誤,并將回溯轉給 sys.stderr 或者其他錯誤數據流。

這個(gè)包裝器也可能會(huì )使用 warnings 模塊生成輸出來(lái)指明存在問(wèn)題但實(shí)際上未被 PEP 3333 所禁止的行為。 除非它們被 Python 命令行選項或 warnings API 所屏蔽,否則任何此類(lèi)警告都將被寫(xiě)入到 sys.stderr (不是 wsgi.errors,除非它們恰好是同一個(gè)對象)。

用法示例:

from wsgiref.validate import validator
from wsgiref.simple_server import make_server

# Our callable object which is intentionally not compliant to the
# standard, so the validator is going to break
def simple_app(environ, start_response):
    status = '200 OK'  # HTTP Status
    headers = [('Content-type', 'text/plain')]  # HTTP Headers
    start_response(status, headers)

    # This is going to break because we need to return a list, and
    # the validator is going to inform us
    return b"Hello World"

# This is the application wrapped in a validator
validator_app = validator(simple_app)

with make_server('', 8000, validator_app) as httpd:
    print("Listening on port 8000....")
    httpd.serve_forever()

wsgiref.handlers -- 服務(wù)器/網(wǎng)關(guān)基類(lèi)?

此模塊提供了用于實(shí)現 WSGI 服務(wù)器和網(wǎng)關(guān)的處理句柄基類(lèi)。 這些基類(lèi)可處理大部分與 WSGI 應用程序通信的工作,只要給予它們一個(gè)帶有輸入、輸出和錯誤流的 CGI 類(lèi)環(huán)境。

class wsgiref.handlers.CGIHandler?

通過(guò) sys.stdin, sys.stdout, sys.stderros.environ 發(fā)起基于 CGI 的調用。 這在當你有一個(gè) WSGI 應用程序并想將其作為 CGI 腳本運行時(shí)很有用處。 只需發(fā)起調用 CGIHandler().run(app),其中 app 是你想要發(fā)起調用的 WSGI 應用程序。

該類(lèi)是 BaseCGIHandler 的子類(lèi),它將設置 wsgi.run_once 為真值,wsgi.multithread 為假值,wsgi.multiprocess 為真值,并總是使用 sysos 來(lái)獲取所需的 CGI 流和環(huán)境。

class wsgiref.handlers.IISCGIHandler?

CGIHandler 的一個(gè)專(zhuān)門(mén)化替代,用于在 Microsoft 的 IIS Web 服務(wù)器上部署,無(wú)需設置 config allowPathInfo 選項 (IIS>=7) 或 metabase allowPathInfoForScriptMappings (IIS<7)。

默認情況下,IIS 給出的 PATH_INFO 會(huì )與前面的 SCRIPT_NAME 重復,導致想要實(shí)現路由的 WSGI 應用程序出現問(wèn)題。 這個(gè)處理句柄會(huì )去除任何這樣的重復路徑。

IIS 可被配置為傳遞正確的 PATH_INFO,但這會(huì )導致另一個(gè) PATH_TRANSLATED 出錯的問(wèn)題。 幸運的是這個(gè)變量很少被使用并且不被 WSGI 所保證。 但是在 IIS<7 上,這個(gè)設置只能在 vhost 層級上進(jìn)行,影響到所有其他腳本映射,其中許多在受 PATH_TRANSLATED 問(wèn)題影響時(shí)都會(huì )中斷運行。 因此 IIS<7 的部署幾乎從不附帶這樣的修正(即使 IIS7 也很少使用它,因為它仍然不帶 UI)。

CGI 代碼沒(méi)有辦法確定該選項是否已設置,因此提供了一個(gè)單獨的處理句柄類(lèi)。 它的用法與 CGIHandler 相同,即通過(guò)調用 IISCGIHandler().run(app),其中 app 是你想要發(fā)起調用的 WSGI 應用程序。

3.2 新版功能.

class wsgiref.handlers.BaseCGIHandler(stdin, stdout, stderr, environ, multithread=True, multiprocess=False)?

類(lèi)似于 CGIHandler,但是改用 sysos 模塊,CGI 環(huán)境和 I/O 流會(huì )被顯式地指定。 multithreadmultiprocess 值被用來(lái)為處理句柄實(shí)例所運行的任何應用程序設置 wsgi.multithreadwsgi.multiprocess 旗標。

該類(lèi)是 SimpleHandler 的子類(lèi),旨在用于 HTTP "原始服務(wù)器" 以外的軟件。 如果你在編寫(xiě)一個(gè)網(wǎng)關(guān)協(xié)議實(shí)現(例如 CGI, FastCGI, SCGI 等等),它使用 Status: 標頭來(lái)發(fā)布 HTTP 狀態(tài),你可能會(huì )想要子類(lèi)化該類(lèi)而不是 SimpleHandler。

class wsgiref.handlers.SimpleHandler(stdin, stdout, stderr, environ, multithread=True, multiprocess=False)?

類(lèi)似于 BaseCGIHandler,但被設計用于 HTTP 原始服務(wù)器。 如果你在編寫(xiě)一個(gè) HTTP 服務(wù)器實(shí)現,你可能會(huì )想要子類(lèi)化該類(lèi)而不是 BaseCGIHandler。

該類(lèi)是 BaseHandler 的子類(lèi)。 它重載了 __init__(), get_stdin(), get_stderr(), add_cgi_vars(), _write()_flush() 方法以支持通過(guò)構造器顯式地設置環(huán)境和流。 所提供的環(huán)境和流存儲在 stdin, stdout, stderrenviron 屬性中。

stdoutwrite() 方法應該完整寫(xiě)入每個(gè)數據塊,與 io.BufferedIOBase 一樣。

class wsgiref.handlers.BaseHandler?

這是適用于運行 WSGI 應用程序的抽象基類(lèi)。 每個(gè)實(shí)例將處理一個(gè)單獨的 HTTP 請求,不過(guò)在原則上你也可以創(chuàng )建一個(gè)可針對多個(gè)請求重用的子類(lèi)。

BaseHandler 實(shí)例只有一個(gè)方法提供給外部使用:

run(app)?

運行指定的 WSGI 應用程序 app。

所有的 BaseHandler 其他方法都是在運行應用程序過(guò)程中由該方法發(fā)起調用的,因此主要是為了允許定制運行過(guò)程。

以下方法必須在子類(lèi)中被重載:

_write(data)?

緩沖字節數據 data 以便傳輸給客戶(hù)端。 如果此方法真的傳輸了數據也是可以的;BaseHandler 只有在底層系統真有這樣的區分時(shí)才會(huì )區分寫(xiě)入和刷新操作以提高效率。

_flush()?

強制將緩沖的數據傳輸給客戶(hù)端。 如果此方法無(wú)任何操作也是可以的(例如,_write() 實(shí)際上已發(fā)送了數據)。

get_stdin()?

Return an object compatible with InputStream suitable for use as the wsgi.input of the request currently being processed.

get_stderr()?

Return an object compatible with ErrorStream suitable for use as the wsgi.errors of the request currently being processed.

add_cgi_vars()?

將當前請求的 CGI 變量插入到 environ 屬性。

以下是一些你可能會(huì )想要重載的其他方法。 但這只是個(gè)簡(jiǎn)略的列表,它不包括每個(gè)可被重載的方法。 你應當在在嘗試創(chuàng )建自定義的 BaseHandler 子類(lèi)之前參閱文檔字符串和源代碼來(lái)了解更多信息。

用于自定義 WSGI 環(huán)境的屬性和方法:

wsgi_multithread?

用于 wsgi.multithread 環(huán)境變量的值。 它在 BaseHandler 中默認為真值,但在其他子類(lèi)中可能有不同的默認值(或是由構造器來(lái)設置)。

wsgi_multiprocess?

用于 wsgi.multiprocess 環(huán)境變量的值。 它在 BaseHandler 中默認為真值,但在其他子類(lèi)中可能有不同的默認值(或是由構造器來(lái)設置)。

wsgi_run_once?

用于 wsgi.run_once 環(huán)境變量的值。 它在 BaseHandler 中默認為假值,但在 CGIHandler 中默認為真值。

os_environ?

要包括在每個(gè)請求的 WSGI 環(huán)境中的默認環(huán)境變量。 在默認情況下,這是當 wsgiref.handlers 被導入時(shí)的 os.environ 的一個(gè)副本,但也可以在類(lèi)或實(shí)例層級上創(chuàng )建它們自己的子類(lèi)。 請注意該字典應當被當作是只讀的,因為默認值會(huì )在多個(gè)類(lèi)和實(shí)際之間共享。

server_software?

如果設置了 origin_server 屬性,該屬性的值會(huì )被用來(lái)設置默認的 SERVER_SOFTWARE WSGI 環(huán)境變量,并且還會(huì )被用來(lái)設置 HTTP 響應中默認的 Server: 標頭。 它會(huì )被不是 HTTP 原始服務(wù)器的處理句柄所忽略 (例如 BaseCGIHandlerCGIHandler)。

在 3.3 版更改: 名稱(chēng) "Python" 會(huì )被替換為實(shí)現專(zhuān)屬的名稱(chēng)如 "CPython", "Jython" 等等。

get_scheme()?

返回當前請求所使用的 URL 方案。 默認的實(shí)現會(huì )使用來(lái)自 wsgiref.utilguess_scheme() 函數根據當前請求的 environ 變量來(lái)猜測方案應該是 "http" 還是 "https"。

setup_environ()?

environ 屬性設為填充了完整內容的 WSGI 環(huán)境。 默認的實(shí)現會(huì )使用上述的所有方法和屬性,加上 get_stdin(), get_stderr()add_cgi_vars() 方法以及 wsgi_file_wrapper 屬性。 它還會(huì )插入一個(gè) SERVER_SOFTWARE 鍵,如果該鍵還不存在的話(huà),只要 origin_server 屬性為真值并且設置了 server_software 屬性。

用于自定義異常處理的方法和屬性:

log_exception(exc_info)?

exc_info 元素記錄到服務(wù)器日志。 exc_info 是一個(gè) (type, value, traceback) 元組。 默認的實(shí)現會(huì )簡(jiǎn)單地將回溯信息寫(xiě)入請求的 wsgi.errors 流并刷新它。 子類(lèi)可以重載此方法來(lái)修改格式或重設輸出目標,通過(guò)郵件將回溯信息發(fā)給管理員,或執行任何其他符合要求的動(dòng)作。

traceback_limit?

要包括在默認 log_exception() 方法的回溯信息輸出中的最大幀數。 如果為 None,則會(huì )包括所有的幀。

error_output(environ, start_response)?

此方法是一個(gè)為用戶(hù)生成錯誤頁(yè)面的 WSGI 應用程序。 它僅當將標頭發(fā)送給客戶(hù)端之前發(fā)生錯誤時(shí)會(huì )被發(fā)起調用。

此方法可使用 sys.exc_info() 來(lái)訪(fǎng)問(wèn)當前錯誤信息,并應當在調用 start_response 時(shí)將該信息傳遞給它(如 PEP 3333 的 "Error Handling" 部分所描述的)。

默認的實(shí)現只是使用 error_status, error_headers, 和 error_body 屬性來(lái)生成一個(gè)輸出頁(yè)面。 子類(lèi)可以重載此方法來(lái)產(chǎn)生更動(dòng)態(tài)化的錯誤輸出。

但是請注意,從安全角度看來(lái)是不建議將診斷信息暴露給任何用戶(hù)的;理想的做法是你應當通過(guò)特別處理來(lái)啟用診斷輸出,因此默認的實(shí)現并不包括這樣的內容。

error_status?

用于錯誤響應的 HTTP 狀態(tài)。 這應當是一個(gè)在 PEP 3333 中定義的字符串;它默認為代碼 500 以相應的消息。

error_headers?

用于錯誤響應的 HTTP 標頭。 這應當是由 WSGI 響應標頭 ((name, value) 元組) 構成的列表,如 PEP 3333 所定義的。 默認的列表只是將內容類(lèi)型設為 text/plain。

error_body?

錯誤響應體。 這應當是一個(gè) HTTP 響應體字節串。 它默認為純文本 "A server error occurred. Please contact the administrator."

用于 PEP 3333 的 "可選的平臺專(zhuān)屬文件處理" 特性的方法和屬性:

wsgi_file_wrapper?

A wsgi.file_wrapper factory, compatible with wsgiref.types.FileWrapper, or None. The default value of this attribute is the wsgiref.util.FileWrapper class.

sendfile()?

重載以實(shí)現平臺專(zhuān)屬的文件傳輸。 此方法僅在應用程序的返回值是由 wsgi_file_wrapper 屬性指定的類(lèi)的實(shí)例時(shí)會(huì )被調用。 如果它能夠成功地傳輸文件則應當返回真值,以使得默認的傳輸代碼將不會(huì )被執行。 此方法的默認實(shí)現只會(huì )返回假值。

雜項方法和屬性:

origin_server?

該屬性在處理句柄的 _write()_flush() 被用于同客戶(hù)端直接通信而不是通過(guò)需要 HTTP 狀態(tài)為某種特殊 Status: 標頭的 CGI 類(lèi)網(wǎng)關(guān)協(xié)議時(shí)應當被設為真值

該屬性在 BaseHandler 中默認為真值,但在 BaseCGIHandlerCGIHandler 中則為假值。

http_version?

如果 origin_server 為真值,則該字符串屬性會(huì )被用來(lái)設置給客戶(hù)端的響應的 HTTP 版本。 它的默認值為 "1.0"。

wsgiref.handlers.read_environ()?

將來(lái)自 os.environ 的 CGI 變量轉碼為 PEP 3333 "bytes in unicode" 字符串,返回一個(gè)新的字典。 此函數被 CGIHandlerIISCGIHandler 用來(lái)替代直接使用 os.environ,后者不一定在所有使用 Python 3 的平臺和 Web 服務(wù)器上都符合 WSGI 標準 -- 特別是當 OS 的實(shí)際環(huán)境為 Unicode 時(shí) (例如 Windows),或者當環(huán)境為字節數據,但被 Python 用來(lái)解碼它的系統編碼格式不是 ISO-8859-1 時(shí) (例如使用 UTF-8 的 Unix 系統)。

如果你要實(shí)現自己的基于 CGI 的處理句柄,你可能會(huì )想要使用此例程而不是簡(jiǎn)單地從 os.environ 直接拷貝值。

3.2 新版功能.

wsgiref.types -- WSGI types for static type checking?

This module provides various types for static type checking as described in PEP 3333.

3.11 新版功能.

class wsgiref.types.StartResponse?

A typing.Protocol describing start_response() callables (PEP 3333).

wsgiref.types.WSGIEnvironment?

A type alias describing a WSGI environment dictionary.

wsgiref.types.WSGIApplication?

A type alias describing a WSGI application callable.

class wsgiref.types.InputStream?

A typing.Protocol describing a WSGI Input Stream.

class wsgiref.types.ErrorStream?

A typing.Protocol describing a WSGI Error Stream.

class wsgiref.types.FileWrapper?

A typing.Protocol describing a file wrapper. See wsgiref.util.FileWrapper for a concrete implementation of this protocol.

例子?

這是一個(gè)可運行的 "Hello World" WSGI 應用程序:

"""
Every WSGI application must have an application object - a callable
object that accepts two arguments. For that purpose, we're going to
use a function (note that you're not limited to a function, you can
use a class for example). The first argument passed to the function
is a dictionary containing CGI-style environment variables and the
second variable is the callable object.
"""
from wsgiref.simple_server import make_server


def hello_world_app(environ, start_response):
    status = "200 OK"  # HTTP Status
    headers = [("Content-type", "text/plain; charset=utf-8")]  # HTTP Headers
    start_response(status, headers)

    # The returned object is going to be printed
    return [b"Hello World"]

with make_server("", 8000, hello_world_app) as httpd:
    print("Serving on port 8000...")

    # Serve until process is killed
    httpd.serve_forever()

Example of a WSGI application serving the current directory, accept optional directory and port number (default: 8000) on the command line:

"""
Small wsgiref based web server. Takes a path to serve from and an
optional port number (defaults to 8000), then tries to serve files.
MIME types are guessed from the file names, 404 errors are raised
if the file is not found.
"""
import mimetypes
import os
import sys
from wsgiref import simple_server, util


def app(environ, respond):
    # Get the file name and MIME type
    fn = os.path.join(path, environ["PATH_INFO"][1:])
    if "." not in fn.split(os.path.sep)[-1]:
        fn = os.path.join(fn, "index.html")
    mime_type = mimetypes.guess_type(fn)[0]

    # Return 200 OK if file exists, otherwise 404 Not Found
    if os.path.exists(fn):
        respond("200 OK", [("Content-Type", mime_type)])
        return util.FileWrapper(open(fn, "rb"))
    else:
        respond("404 Not Found", [("Content-Type", "text/plain")])
        return [b"not found"]


if __name__ == "__main__":
    # Get the path and port from command-line arguments
    path = sys.argv[1] if len(sys.argv) > 1 else os.getcwd()
    port = int(sys.argv[2]) if len(sys.argv) > 2 else 8000

    # Make and start the server until control-c
    httpd = simple_server.make_server("", port, app)
    print(f"Serving {path} on port {port}, control-C to stop")
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        print("Shutting down.")
        httpd.server_close()