當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python wsgiref.validate.validator用法及代碼示例

用法:

wsgiref.validate.validator(application)

application並返回一個新的 WSGI 應用程序對象。返回的應用程序會將所有請求轉發給原始應用程序application, 並將檢查兩者application並且調用它的服務器符合 WSGI 規範和RFC 2616.

任何檢測到的不符合項都會引發AssertionError;但是請注意,如何處理這些錯誤是server-dependent。例如,wsgiref.simple_server 和其他基於wsgiref.handlers 的服務器(不覆蓋錯誤處理方法來做其他事情)將簡單地輸出一條錯誤發生的消息,並將回溯轉儲到sys.stderr 或一些其他錯誤流。

這個包裝器也可以使用warnings模塊來指示有問題但實際上可能未被禁止的行為 PEP 3333.除非使用 Python 命令行 選項或warningsAPI,任何此類警告都將寫入sys.stderr(not wsgi.errors,除非它們碰巧是同一個對象)。

示例用法:

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()

相關用法


注:本文由純淨天空篩選整理自python.org大神的英文原創作品 wsgiref.validate.validator。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。