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


Python wsgiref.util.setup_testing_defaults用法及代碼示例

用法:

wsgiref.util.setup_testing_defaults(environ)

使用簡單的默認值更新environ 以進行測試。

該例程添加了 WSGI 所需的各種參數,包括HTTP_HOST,SERVER_NAME,SERVER_PORT,REQUEST_METHOD,SCRIPT_NAME,PATH_INFO,以及所有的 PEP 3333-定義wsgi.*變量。它僅提供默認值,不會替換這些變量的任何現有設置。

該例程旨在使 WSGI 服務器和應用程序的單元測試更容易設置虛擬環境。它不應該被實際的 WSGI 服務器或應用程序使用,因為數據是假的!

示例用法:

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

相關用法


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