wsgiref
— WSGI 实用程序和参考实现
¶
源代码: Lib/wsgiref
The Web Server Gateway Interface (WSGI) is a standard interface between web server software and web applications written in Python. Having a standard interface makes it easy to use an application that supports WSGI with a number of different web servers.
Only authors of web servers and programming frameworks need to know every detail and corner case of the WSGI design. You don’t need to understand every detail of WSGI just to install a WSGI application or to write a web application using an existing framework.
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 for more information about WSGI, and links to tutorials and other resources.
wsgiref.util
– WSGI 环境实用程序
¶
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 ) ¶
-
Return a guess for whether
wsgi.url_schemeshould be “http” or “https”, by checking for aHTTPSenvironment variable in the environ dictionary. The return value is a string.This function is useful when creating a gateway that wraps CGI or a CGI-like protocol such as FastCGI. Typically, servers providing such protocols will include a
HTTPSvariable with a value of “1”, “yes”, or “on” when a request is received via SSL. So, this function returns “https” if such a value is found, and “http” otherwise.
- wsgiref.util. request_uri ( environ , include_query = True ) ¶
-
Return the full request URI, optionally including the query string, using the algorithm found in the “URL Reconstruction” section of PEP 3333 。若 include_query is false, the query string is not included in the resulting URI.
- wsgiref.util. application_uri ( environ ) ¶
-
类似于
request_uri(), except that thePATH_INFOandQUERY_STRINGvariables are ignored. The result is the base URI of the application object addressed by the request.
- wsgiref.util. shift_path_info ( environ ) ¶
-
Shift a single name from
PATH_INFOtoSCRIPT_NAMEand return the name. The environ dictionary is modified in-place; use a copy if you need to keep the originalPATH_INFOorSCRIPT_NAMEintact.If there are no remaining path segments in
PATH_INFO,None被返回。Typically, this routine is used to process each portion of a request URI path, for example to treat the path as a series of dictionary keys. This routine modifies the passed-in environment to make it suitable for invoking another WSGI application that is located at the target URI. For example, if there is a WSGI application at
/foo, and the request URI path is/foo/bar/baz, and the WSGI application at/foo调用shift_path_info(), it will receive the string “bar”, and the environment will be updated to be suitable for passing to a WSGI application at/foo/bar. That is,SCRIPT_NAMEwill change from/footo/foo/bar,和PATH_INFOwill change from/bar/bazto/baz.当
PATH_INFOis just a “/”, this routine returns an empty string and appends a trailing slash toSCRIPT_NAME, even though empty path segments are normally ignored, andSCRIPT_NAMEdoesn’t normally end in a slash. This is intentional behavior, to ensure that an application can tell the difference between URIs ending in/xfrom ones ending in/x/when using this routine to do object traversal.
- wsgiref.util. setup_testing_defaults ( environ ) ¶
-
更新 environ with trivial defaults for testing purposes.
This routine adds various parameters required for WSGI, including
HTTP_HOST,SERVER_NAME,SERVER_PORT,REQUEST_METHOD,SCRIPT_NAME,PATH_INFO, and all of the PEP 3333 定义wsgi.*variables. It only supplies default values, and does not replace any existing settings for these variables.This routine is intended to make it easier for unit tests of WSGI servers and applications to set up dummy environments. It should NOT be used by actual WSGI servers or applications, since the data is fake!
用法范例:
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()