| 1 | \section{\module{wsgiref} --- WSGI Utilities and Reference
|
|---|
| 2 | Implementation}
|
|---|
| 3 | \declaremodule{}{wsgiref}
|
|---|
| 4 | \moduleauthor{Phillip J. Eby}{[email protected]}
|
|---|
| 5 | \sectionauthor{Phillip J. Eby}{[email protected]}
|
|---|
| 6 | \modulesynopsis{WSGI Utilities and Reference Implementation}
|
|---|
| 7 |
|
|---|
| 8 | \versionadded{2.5}
|
|---|
| 9 |
|
|---|
| 10 | The Web Server Gateway Interface (WSGI) is a standard interface
|
|---|
| 11 | between web server software and web applications written in Python.
|
|---|
| 12 | Having a standard interface makes it easy to use an application
|
|---|
| 13 | that supports WSGI with a number of different web servers.
|
|---|
| 14 |
|
|---|
| 15 | Only authors of web servers and programming frameworks need to know
|
|---|
| 16 | every detail and corner case of the WSGI design. You don't need to
|
|---|
| 17 | understand every detail of WSGI just to install a WSGI application or
|
|---|
| 18 | to write a web application using an existing framework.
|
|---|
| 19 |
|
|---|
| 20 | \module{wsgiref} is a reference implementation of the WSGI specification
|
|---|
| 21 | that can be used to add WSGI support to a web server or framework. It
|
|---|
| 22 | provides utilities for manipulating WSGI environment variables and
|
|---|
| 23 | response headers, base classes for implementing WSGI servers, a demo
|
|---|
| 24 | HTTP server that serves WSGI applications, and a validation tool that
|
|---|
| 25 | checks WSGI servers and applications for conformance to the
|
|---|
| 26 | WSGI specification (\pep{333}).
|
|---|
| 27 |
|
|---|
| 28 | % XXX If you're just trying to write a web application...
|
|---|
| 29 | % XXX should create a URL on python.org to point people to.
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | \subsection{\module{wsgiref.util} -- WSGI environment utilities}
|
|---|
| 45 | \declaremodule{}{wsgiref.util}
|
|---|
| 46 |
|
|---|
| 47 | This module provides a variety of utility functions for working with
|
|---|
| 48 | WSGI environments. A WSGI environment is a dictionary containing
|
|---|
| 49 | HTTP request variables as described in \pep{333}. All of the functions
|
|---|
| 50 | taking an \var{environ} parameter expect a WSGI-compliant dictionary to
|
|---|
| 51 | be supplied; please see \pep{333} for a detailed specification.
|
|---|
| 52 |
|
|---|
| 53 | \begin{funcdesc}{guess_scheme}{environ}
|
|---|
| 54 | Return a guess for whether \code{wsgi.url_scheme} should be ``http'' or
|
|---|
| 55 | ``https'', by checking for a \code{HTTPS} environment variable in the
|
|---|
| 56 | \var{environ} dictionary. The return value is a string.
|
|---|
| 57 |
|
|---|
| 58 | This function is useful when creating a gateway that wraps CGI or a
|
|---|
| 59 | CGI-like protocol such as FastCGI. Typically, servers providing such
|
|---|
| 60 | protocols will include a \code{HTTPS} variable with a value of ``1''
|
|---|
| 61 | ``yes'', or ``on'' when a request is received via SSL. So, this
|
|---|
| 62 | function returns ``https'' if such a value is found, and ``http''
|
|---|
| 63 | otherwise.
|
|---|
| 64 | \end{funcdesc}
|
|---|
| 65 |
|
|---|
| 66 | \begin{funcdesc}{request_uri}{environ \optional{, include_query=1}}
|
|---|
| 67 | Return the full request URI, optionally including the query string,
|
|---|
| 68 | using the algorithm found in the ``URL Reconstruction'' section of
|
|---|
| 69 | \pep{333}. If \var{include_query} is false, the query string is
|
|---|
| 70 | not included in the resulting URI.
|
|---|
| 71 | \end{funcdesc}
|
|---|
| 72 |
|
|---|
| 73 | \begin{funcdesc}{application_uri}{environ}
|
|---|
| 74 | Similar to \function{request_uri}, except that the \code{PATH_INFO} and
|
|---|
| 75 | \code{QUERY_STRING} variables are ignored. The result is the base URI
|
|---|
| 76 | of the application object addressed by the request.
|
|---|
| 77 | \end{funcdesc}
|
|---|
| 78 |
|
|---|
| 79 | \begin{funcdesc}{shift_path_info}{environ}
|
|---|
| 80 | Shift a single name from \code{PATH_INFO} to \code{SCRIPT_NAME} and
|
|---|
| 81 | return the name. The \var{environ} dictionary is \emph{modified}
|
|---|
| 82 | in-place; use a copy if you need to keep the original \code{PATH_INFO}
|
|---|
| 83 | or \code{SCRIPT_NAME} intact.
|
|---|
| 84 |
|
|---|
| 85 | If there are no remaining path segments in \code{PATH_INFO}, \code{None}
|
|---|
| 86 | is returned.
|
|---|
| 87 |
|
|---|
| 88 | Typically, this routine is used to process each portion of a request
|
|---|
| 89 | URI path, for example to treat the path as a series of dictionary keys.
|
|---|
| 90 | This routine modifies the passed-in environment to make it suitable for
|
|---|
| 91 | invoking another WSGI application that is located at the target URI.
|
|---|
| 92 | For example, if there is a WSGI application at \code{/foo}, and the
|
|---|
| 93 | request URI path is \code{/foo/bar/baz}, and the WSGI application at
|
|---|
| 94 | \code{/foo} calls \function{shift_path_info}, it will receive the string
|
|---|
| 95 | ``bar'', and the environment will be updated to be suitable for passing
|
|---|
| 96 | to a WSGI application at \code{/foo/bar}. That is, \code{SCRIPT_NAME}
|
|---|
| 97 | will change from \code{/foo} to \code{/foo/bar}, and \code{PATH_INFO}
|
|---|
| 98 | will change from \code{/bar/baz} to \code{/baz}.
|
|---|
| 99 |
|
|---|
| 100 | When \code{PATH_INFO} is just a ``/'', this routine returns an empty
|
|---|
| 101 | string and appends a trailing slash to \code{SCRIPT_NAME}, even though
|
|---|
| 102 | empty path segments are normally ignored, and \code{SCRIPT_NAME} doesn't
|
|---|
| 103 | normally end in a slash. This is intentional behavior, to ensure that
|
|---|
| 104 | an application can tell the difference between URIs ending in \code{/x}
|
|---|
| 105 | from ones ending in \code{/x/} when using this routine to do object
|
|---|
| 106 | traversal.
|
|---|
| 107 |
|
|---|
| 108 | \end{funcdesc}
|
|---|
| 109 |
|
|---|
| 110 | \begin{funcdesc}{setup_testing_defaults}{environ}
|
|---|
| 111 | Update \var{environ} with trivial defaults for testing purposes.
|
|---|
| 112 |
|
|---|
| 113 | This routine adds various parameters required for WSGI, including
|
|---|
| 114 | \code{HTTP_HOST}, \code{SERVER_NAME}, \code{SERVER_PORT},
|
|---|
| 115 | \code{REQUEST_METHOD}, \code{SCRIPT_NAME}, \code{PATH_INFO}, and all of
|
|---|
| 116 | the \pep{333}-defined \code{wsgi.*} variables. It only supplies default
|
|---|
| 117 | values, and does not replace any existing settings for these variables.
|
|---|
| 118 |
|
|---|
| 119 | This routine is intended to make it easier for unit tests of WSGI
|
|---|
| 120 | servers and applications to set up dummy environments. It should NOT
|
|---|
| 121 | be used by actual WSGI servers or applications, since the data is fake!
|
|---|
| 122 | \end{funcdesc}
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 | In addition to the environment functions above, the
|
|---|
| 127 | \module{wsgiref.util} module also provides these miscellaneous
|
|---|
| 128 | utilities:
|
|---|
| 129 |
|
|---|
| 130 | \begin{funcdesc}{is_hop_by_hop}{header_name}
|
|---|
| 131 | Return true if 'header_name' is an HTTP/1.1 ``Hop-by-Hop'' header, as
|
|---|
| 132 | defined by \rfc{2616}.
|
|---|
| 133 | \end{funcdesc}
|
|---|
| 134 |
|
|---|
| 135 | \begin{classdesc}{FileWrapper}{filelike \optional{, blksize=8192}}
|
|---|
| 136 | A wrapper to convert a file-like object to an iterator. The resulting
|
|---|
| 137 | objects support both \method{__getitem__} and \method{__iter__}
|
|---|
| 138 | iteration styles, for compatibility with Python 2.1 and Jython.
|
|---|
| 139 | As the object is iterated over, the optional \var{blksize} parameter
|
|---|
| 140 | will be repeatedly passed to the \var{filelike} object's \method{read()}
|
|---|
| 141 | method to obtain strings to yield. When \method{read()} returns an
|
|---|
| 142 | empty string, iteration is ended and is not resumable.
|
|---|
| 143 |
|
|---|
| 144 | If \var{filelike} has a \method{close()} method, the returned object
|
|---|
| 145 | will also have a \method{close()} method, and it will invoke the
|
|---|
| 146 | \var{filelike} object's \method{close()} method when called.
|
|---|
| 147 | \end{classdesc}
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 | \subsection{\module{wsgiref.headers} -- WSGI response header tools}
|
|---|
| 168 | \declaremodule{}{wsgiref.headers}
|
|---|
| 169 |
|
|---|
| 170 | This module provides a single class, \class{Headers}, for convenient
|
|---|
| 171 | manipulation of WSGI response headers using a mapping-like interface.
|
|---|
| 172 |
|
|---|
| 173 | \begin{classdesc}{Headers}{headers}
|
|---|
| 174 | Create a mapping-like object wrapping \var{headers}, which must be a
|
|---|
| 175 | list of header name/value tuples as described in \pep{333}. Any changes
|
|---|
| 176 | made to the new \class{Headers} object will directly update the
|
|---|
| 177 | \var{headers} list it was created with.
|
|---|
| 178 |
|
|---|
| 179 | \class{Headers} objects support typical mapping operations including
|
|---|
| 180 | \method{__getitem__}, \method{get}, \method{__setitem__},
|
|---|
| 181 | \method{setdefault}, \method{__delitem__}, \method{__contains__} and
|
|---|
| 182 | \method{has_key}. For each of these methods, the key is the header name
|
|---|
| 183 | (treated case-insensitively), and the value is the first value
|
|---|
| 184 | associated with that header name. Setting a header deletes any existing
|
|---|
| 185 | values for that header, then adds a new value at the end of the wrapped
|
|---|
| 186 | header list. Headers' existing order is generally maintained, with new
|
|---|
| 187 | headers added to the end of the wrapped list.
|
|---|
| 188 |
|
|---|
| 189 | Unlike a dictionary, \class{Headers} objects do not raise an error when
|
|---|
| 190 | you try to get or delete a key that isn't in the wrapped header list.
|
|---|
| 191 | Getting a nonexistent header just returns \code{None}, and deleting
|
|---|
| 192 | a nonexistent header does nothing.
|
|---|
| 193 |
|
|---|
| 194 | \class{Headers} objects also support \method{keys()}, \method{values()},
|
|---|
| 195 | and \method{items()} methods. The lists returned by \method{keys()}
|
|---|
| 196 | and \method{items()} can include the same key more than once if there
|
|---|
| 197 | is a multi-valued header. The \code{len()} of a \class{Headers} object
|
|---|
| 198 | is the same as the length of its \method{items()}, which is the same
|
|---|
| 199 | as the length of the wrapped header list. In fact, the \method{items()}
|
|---|
| 200 | method just returns a copy of the wrapped header list.
|
|---|
| 201 |
|
|---|
| 202 | Calling \code{str()} on a \class{Headers} object returns a formatted
|
|---|
| 203 | string suitable for transmission as HTTP response headers. Each header
|
|---|
| 204 | is placed on a line with its value, separated by a colon and a space.
|
|---|
| 205 | Each line is terminated by a carriage return and line feed, and the
|
|---|
| 206 | string is terminated with a blank line.
|
|---|
| 207 |
|
|---|
| 208 | In addition to their mapping interface and formatting features,
|
|---|
| 209 | \class{Headers} objects also have the following methods for querying
|
|---|
| 210 | and adding multi-valued headers, and for adding headers with MIME
|
|---|
| 211 | parameters:
|
|---|
| 212 |
|
|---|
| 213 | \begin{methoddesc}{get_all}{name}
|
|---|
| 214 | Return a list of all the values for the named header.
|
|---|
| 215 |
|
|---|
| 216 | The returned list will be sorted in the order they appeared in the
|
|---|
| 217 | original header list or were added to this instance, and may contain
|
|---|
| 218 | duplicates. Any fields deleted and re-inserted are always appended to
|
|---|
| 219 | the header list. If no fields exist with the given name, returns an
|
|---|
| 220 | empty list.
|
|---|
| 221 | \end{methoddesc}
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 | \begin{methoddesc}{add_header}{name, value, **_params}
|
|---|
| 225 | Add a (possibly multi-valued) header, with optional MIME parameters
|
|---|
| 226 | specified via keyword arguments.
|
|---|
| 227 |
|
|---|
| 228 | \var{name} is the header field to add. Keyword arguments can be used to
|
|---|
| 229 | set MIME parameters for the header field. Each parameter must be a
|
|---|
| 230 | string or \code{None}. Underscores in parameter names are converted to
|
|---|
| 231 | dashes, since dashes are illegal in Python identifiers, but many MIME
|
|---|
| 232 | parameter names include dashes. If the parameter value is a string, it
|
|---|
| 233 | is added to the header value parameters in the form \code{name="value"}.
|
|---|
| 234 | If it is \code{None}, only the parameter name is added. (This is used
|
|---|
| 235 | for MIME parameters without a value.) Example usage:
|
|---|
| 236 |
|
|---|
| 237 | \begin{verbatim}
|
|---|
| 238 | h.add_header('content-disposition', 'attachment', filename='bud.gif')
|
|---|
| 239 | \end{verbatim}
|
|---|
| 240 |
|
|---|
| 241 | The above will add a header that looks like this:
|
|---|
| 242 |
|
|---|
| 243 | \begin{verbatim}
|
|---|
| 244 | Content-Disposition: attachment; filename="bud.gif"
|
|---|
| 245 | \end{verbatim}
|
|---|
| 246 | \end{methoddesc}
|
|---|
| 247 | \end{classdesc}
|
|---|
| 248 |
|
|---|
| 249 | \subsection{\module{wsgiref.simple_server} -- a simple WSGI HTTP server}
|
|---|
| 250 | \declaremodule[wsgiref.simpleserver]{}{wsgiref.simple_server}
|
|---|
| 251 |
|
|---|
| 252 | This module implements a simple HTTP server (based on
|
|---|
| 253 | \module{BaseHTTPServer}) that serves WSGI applications. Each server
|
|---|
| 254 | instance serves a single WSGI application on a given host and port. If
|
|---|
| 255 | you want to serve multiple applications on a single host and port, you
|
|---|
| 256 | should create a WSGI application that parses \code{PATH_INFO} to select
|
|---|
| 257 | which application to invoke for each request. (E.g., using the
|
|---|
| 258 | \function{shift_path_info()} function from \module{wsgiref.util}.)
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 | \begin{funcdesc}{make_server}{host, port, app
|
|---|
| 262 | \optional{, server_class=\class{WSGIServer} \optional{,
|
|---|
| 263 | handler_class=\class{WSGIRequestHandler}}}}
|
|---|
| 264 | Create a new WSGI server listening on \var{host} and \var{port},
|
|---|
| 265 | accepting connections for \var{app}. The return value is an instance of
|
|---|
| 266 | the supplied \var{server_class}, and will process requests using the
|
|---|
| 267 | specified \var{handler_class}. \var{app} must be a WSGI application
|
|---|
| 268 | object, as defined by \pep{333}.
|
|---|
| 269 |
|
|---|
| 270 | Example usage:
|
|---|
| 271 | \begin{verbatim}from wsgiref.simple_server import make_server, demo_app
|
|---|
| 272 |
|
|---|
| 273 | httpd = make_server('', 8000, demo_app)
|
|---|
| 274 | print "Serving HTTP on port 8000..."
|
|---|
| 275 |
|
|---|
| 276 | # Respond to requests until process is killed
|
|---|
| 277 | httpd.serve_forever()
|
|---|
| 278 |
|
|---|
| 279 | # Alternative: serve one request, then exit
|
|---|
| 280 | ##httpd.handle_request()
|
|---|
| 281 | \end{verbatim}
|
|---|
| 282 |
|
|---|
| 283 | \end{funcdesc}
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 | \begin{funcdesc}{demo_app}{environ, start_response}
|
|---|
| 291 | This function is a small but complete WSGI application that
|
|---|
| 292 | returns a text page containing the message ``Hello world!''
|
|---|
| 293 | and a list of the key/value pairs provided in the
|
|---|
| 294 | \var{environ} parameter. It's useful for verifying that a WSGI server
|
|---|
| 295 | (such as \module{wsgiref.simple_server}) is able to run a simple WSGI
|
|---|
| 296 | application correctly.
|
|---|
| 297 | \end{funcdesc}
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 | \begin{classdesc}{WSGIServer}{server_address, RequestHandlerClass}
|
|---|
| 301 | Create a \class{WSGIServer} instance. \var{server_address} should be
|
|---|
| 302 | a \code{(host,port)} tuple, and \var{RequestHandlerClass} should be
|
|---|
| 303 | the subclass of \class{BaseHTTPServer.BaseHTTPRequestHandler} that will
|
|---|
| 304 | be used to process requests.
|
|---|
| 305 |
|
|---|
| 306 | You do not normally need to call this constructor, as the
|
|---|
| 307 | \function{make_server()} function can handle all the details for you.
|
|---|
| 308 |
|
|---|
| 309 | \class{WSGIServer} is a subclass
|
|---|
| 310 | of \class{BaseHTTPServer.HTTPServer}, so all of its methods (such as
|
|---|
| 311 | \method{serve_forever()} and \method{handle_request()}) are available.
|
|---|
| 312 | \class{WSGIServer} also provides these WSGI-specific methods:
|
|---|
| 313 |
|
|---|
| 314 | \begin{methoddesc}{set_app}{application}
|
|---|
| 315 | Sets the callable \var{application} as the WSGI application that will
|
|---|
| 316 | receive requests.
|
|---|
| 317 | \end{methoddesc}
|
|---|
| 318 |
|
|---|
| 319 | \begin{methoddesc}{get_app}{}
|
|---|
| 320 | Returns the currently-set application callable.
|
|---|
| 321 | \end{methoddesc}
|
|---|
| 322 |
|
|---|
| 323 | Normally, however, you do not need to use these additional methods, as
|
|---|
| 324 | \method{set_app()} is normally called by \function{make_server()}, and
|
|---|
| 325 | the \method{get_app()} exists mainly for the benefit of request handler
|
|---|
| 326 | instances.
|
|---|
| 327 | \end{classdesc}
|
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 | \begin{classdesc}{WSGIRequestHandler}{request, client_address, server}
|
|---|
| 332 | Create an HTTP handler for the given \var{request} (i.e. a socket),
|
|---|
| 333 | \var{client_address} (a \code{(\var{host},\var{port})} tuple), and
|
|---|
| 334 | \var{server} (\class{WSGIServer} instance).
|
|---|
| 335 |
|
|---|
| 336 | You do not need to create instances of this class directly; they are
|
|---|
| 337 | automatically created as needed by \class{WSGIServer} objects. You
|
|---|
| 338 | can, however, subclass this class and supply it as a \var{handler_class}
|
|---|
| 339 | to the \function{make_server()} function. Some possibly relevant
|
|---|
| 340 | methods for overriding in subclasses:
|
|---|
| 341 |
|
|---|
| 342 | \begin{methoddesc}{get_environ}{}
|
|---|
| 343 | Returns a dictionary containing the WSGI environment for a request. The
|
|---|
| 344 | default implementation copies the contents of the \class{WSGIServer}
|
|---|
| 345 | object's \member{base_environ} dictionary attribute and then adds
|
|---|
| 346 | various headers derived from the HTTP request. Each call to this method
|
|---|
| 347 | should return a new dictionary containing all of the relevant CGI
|
|---|
| 348 | environment variables as specified in \pep{333}.
|
|---|
| 349 | \end{methoddesc}
|
|---|
| 350 |
|
|---|
| 351 | \begin{methoddesc}{get_stderr}{}
|
|---|
| 352 | Return the object that should be used as the \code{wsgi.errors} stream.
|
|---|
| 353 | The default implementation just returns \code{sys.stderr}.
|
|---|
| 354 | \end{methoddesc}
|
|---|
| 355 |
|
|---|
| 356 | \begin{methoddesc}{handle}{}
|
|---|
| 357 | Process the HTTP request. The default implementation creates a handler
|
|---|
| 358 | instance using a \module{wsgiref.handlers} class to implement the actual
|
|---|
| 359 | WSGI application interface.
|
|---|
| 360 | \end{methoddesc}
|
|---|
| 361 |
|
|---|
| 362 | \end{classdesc}
|
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 | \subsection{\module{wsgiref.validate} -- WSGI conformance checker}
|
|---|
| 373 | \declaremodule{}{wsgiref.validate}
|
|---|
| 374 | When creating new WSGI application objects, frameworks, servers, or
|
|---|
| 375 | middleware, it can be useful to validate the new code's conformance
|
|---|
| 376 | using \module{wsgiref.validate}. This module provides a function that
|
|---|
| 377 | creates WSGI application objects that validate communications between
|
|---|
| 378 | a WSGI server or gateway and a WSGI application object, to check both
|
|---|
| 379 | sides for protocol conformance.
|
|---|
| 380 |
|
|---|
| 381 | Note that this utility does not guarantee complete \pep{333} compliance;
|
|---|
| 382 | an absence of errors from this module does not necessarily mean that
|
|---|
| 383 | errors do not exist. However, if this module does produce an error,
|
|---|
| 384 | then it is virtually certain that either the server or application is
|
|---|
| 385 | not 100\% compliant.
|
|---|
| 386 |
|
|---|
| 387 | This module is based on the \module{paste.lint} module from Ian
|
|---|
| 388 | Bicking's ``Python Paste'' library.
|
|---|
| 389 |
|
|---|
| 390 | \begin{funcdesc}{validator}{application}
|
|---|
| 391 | Wrap \var{application} and return a new WSGI application object. The
|
|---|
| 392 | returned application will forward all requests to the original
|
|---|
| 393 | \var{application}, and will check that both the \var{application} and
|
|---|
| 394 | the server invoking it are conforming to the WSGI specification and to
|
|---|
| 395 | RFC 2616.
|
|---|
| 396 |
|
|---|
| 397 | Any detected nonconformance results in an \exception{AssertionError}
|
|---|
| 398 | being raised; note, however, that how these errors are handled is
|
|---|
| 399 | server-dependent. For example, \module{wsgiref.simple_server} and other
|
|---|
| 400 | servers based on \module{wsgiref.handlers} (that don't override the
|
|---|
| 401 | error handling methods to do something else) will simply output a
|
|---|
| 402 | message that an error has occurred, and dump the traceback to
|
|---|
| 403 | \code{sys.stderr} or some other error stream.
|
|---|
| 404 |
|
|---|
| 405 | This wrapper may also generate output using the \module{warnings} module
|
|---|
| 406 | to indicate behaviors that are questionable but which may not actually
|
|---|
| 407 | be prohibited by \pep{333}. Unless they are suppressed using Python
|
|---|
| 408 | command-line options or the \module{warnings} API, any such warnings
|
|---|
| 409 | will be written to \code{sys.stderr} (\emph{not} \code{wsgi.errors},
|
|---|
| 410 | unless they happen to be the same object).
|
|---|
| 411 | \end{funcdesc}
|
|---|
| 412 |
|
|---|
| 413 | \subsection{\module{wsgiref.handlers} -- server/gateway base classes}
|
|---|
| 414 | \declaremodule{}{wsgiref.handlers}
|
|---|
| 415 |
|
|---|
| 416 | This module provides base handler classes for implementing WSGI servers
|
|---|
| 417 | and gateways. These base classes handle most of the work of
|
|---|
| 418 | communicating with a WSGI application, as long as they are given a
|
|---|
| 419 | CGI-like environment, along with input, output, and error streams.
|
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 | \begin{classdesc}{CGIHandler}{}
|
|---|
| 423 | CGI-based invocation via \code{sys.stdin}, \code{sys.stdout},
|
|---|
| 424 | \code{sys.stderr} and \code{os.environ}. This is useful when you have
|
|---|
| 425 | a WSGI application and want to run it as a CGI script. Simply invoke
|
|---|
| 426 | \code{CGIHandler().run(app)}, where \code{app} is the WSGI application
|
|---|
| 427 | object you wish to invoke.
|
|---|
| 428 |
|
|---|
| 429 | This class is a subclass of \class{BaseCGIHandler} that sets
|
|---|
| 430 | \code{wsgi.run_once} to true, \code{wsgi.multithread} to false, and
|
|---|
| 431 | \code{wsgi.multiprocess} to true, and always uses \module{sys} and
|
|---|
| 432 | \module{os} to obtain the necessary CGI streams and environment.
|
|---|
| 433 | \end{classdesc}
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 | \begin{classdesc}{BaseCGIHandler}{stdin, stdout, stderr, environ
|
|---|
| 437 | \optional{, multithread=True \optional{, multiprocess=False}}}
|
|---|
| 438 |
|
|---|
| 439 | Similar to \class{CGIHandler}, but instead of using the \module{sys} and
|
|---|
| 440 | \module{os} modules, the CGI environment and I/O streams are specified
|
|---|
| 441 | explicitly. The \var{multithread} and \var{multiprocess} values are
|
|---|
| 442 | used to set the \code{wsgi.multithread} and \code{wsgi.multiprocess}
|
|---|
| 443 | flags for any applications run by the handler instance.
|
|---|
| 444 |
|
|---|
| 445 | This class is a subclass of \class{SimpleHandler} intended for use with
|
|---|
| 446 | software other than HTTP ``origin servers''. If you are writing a
|
|---|
| 447 | gateway protocol implementation (such as CGI, FastCGI, SCGI, etc.) that
|
|---|
| 448 | uses a \code{Status:} header to send an HTTP status, you probably want
|
|---|
| 449 | to subclass this instead of \class{SimpleHandler}.
|
|---|
| 450 | \end{classdesc}
|
|---|
| 451 |
|
|---|
| 452 |
|
|---|
| 453 |
|
|---|
| 454 | \begin{classdesc}{SimpleHandler}{stdin, stdout, stderr, environ
|
|---|
| 455 | \optional{,multithread=True \optional{, multiprocess=False}}}
|
|---|
| 456 |
|
|---|
| 457 | Similar to \class{BaseCGIHandler}, but designed for use with HTTP origin
|
|---|
| 458 | servers. If you are writing an HTTP server implementation, you will
|
|---|
| 459 | probably want to subclass this instead of \class{BaseCGIHandler}
|
|---|
| 460 |
|
|---|
| 461 | This class is a subclass of \class{BaseHandler}. It overrides the
|
|---|
| 462 | \method{__init__()}, \method{get_stdin()}, \method{get_stderr()},
|
|---|
| 463 | \method{add_cgi_vars()}, \method{_write()}, and \method{_flush()}
|
|---|
| 464 | methods to support explicitly setting the environment and streams via
|
|---|
| 465 | the constructor. The supplied environment and streams are stored in
|
|---|
| 466 | the \member{stdin}, \member{stdout}, \member{stderr}, and
|
|---|
| 467 | \member{environ} attributes.
|
|---|
| 468 | \end{classdesc}
|
|---|
| 469 |
|
|---|
| 470 | \begin{classdesc}{BaseHandler}{}
|
|---|
| 471 | This is an abstract base class for running WSGI applications. Each
|
|---|
| 472 | instance will handle a single HTTP request, although in principle you
|
|---|
| 473 | could create a subclass that was reusable for multiple requests.
|
|---|
| 474 |
|
|---|
| 475 | \class{BaseHandler} instances have only one method intended for external
|
|---|
| 476 | use:
|
|---|
| 477 |
|
|---|
| 478 | \begin{methoddesc}{run}{app}
|
|---|
| 479 | Run the specified WSGI application, \var{app}.
|
|---|
| 480 | \end{methoddesc}
|
|---|
| 481 |
|
|---|
| 482 | All of the other \class{BaseHandler} methods are invoked by this method
|
|---|
| 483 | in the process of running the application, and thus exist primarily to
|
|---|
| 484 | allow customizing the process.
|
|---|
| 485 |
|
|---|
| 486 | The following methods MUST be overridden in a subclass:
|
|---|
| 487 |
|
|---|
| 488 | \begin{methoddesc}{_write}{data}
|
|---|
| 489 | Buffer the string \var{data} for transmission to the client. It's okay
|
|---|
| 490 | if this method actually transmits the data; \class{BaseHandler}
|
|---|
| 491 | just separates write and flush operations for greater efficiency
|
|---|
| 492 | when the underlying system actually has such a distinction.
|
|---|
| 493 | \end{methoddesc}
|
|---|
| 494 |
|
|---|
| 495 | \begin{methoddesc}{_flush}{}
|
|---|
| 496 | Force buffered data to be transmitted to the client. It's okay if this
|
|---|
| 497 | method is a no-op (i.e., if \method{_write()} actually sends the data).
|
|---|
| 498 | \end{methoddesc}
|
|---|
| 499 |
|
|---|
| 500 | \begin{methoddesc}{get_stdin}{}
|
|---|
| 501 | Return an input stream object suitable for use as the \code{wsgi.input}
|
|---|
| 502 | of the request currently being processed.
|
|---|
| 503 | \end{methoddesc}
|
|---|
| 504 |
|
|---|
| 505 | \begin{methoddesc}{get_stderr}{}
|
|---|
| 506 | Return an output stream object suitable for use as the
|
|---|
| 507 | \code{wsgi.errors} of the request currently being processed.
|
|---|
| 508 | \end{methoddesc}
|
|---|
| 509 |
|
|---|
| 510 | \begin{methoddesc}{add_cgi_vars}{}
|
|---|
| 511 | Insert CGI variables for the current request into the \member{environ}
|
|---|
| 512 | attribute.
|
|---|
| 513 | \end{methoddesc}
|
|---|
| 514 |
|
|---|
| 515 | Here are some other methods and attributes you may wish to override.
|
|---|
| 516 | This list is only a summary, however, and does not include every method
|
|---|
| 517 | that can be overridden. You should consult the docstrings and source
|
|---|
| 518 | code for additional information before attempting to create a customized
|
|---|
| 519 | \class{BaseHandler} subclass.
|
|---|
| 520 |
|
|---|
| 521 |
|
|---|
| 522 |
|
|---|
| 523 |
|
|---|
| 524 |
|
|---|
| 525 |
|
|---|
| 526 |
|
|---|
| 527 |
|
|---|
| 528 |
|
|---|
| 529 |
|
|---|
| 530 |
|
|---|
| 531 |
|
|---|
| 532 |
|
|---|
| 533 |
|
|---|
| 534 |
|
|---|
| 535 |
|
|---|
| 536 | Attributes and methods for customizing the WSGI environment:
|
|---|
| 537 |
|
|---|
| 538 | \begin{memberdesc}{wsgi_multithread}
|
|---|
| 539 | The value to be used for the \code{wsgi.multithread} environment
|
|---|
| 540 | variable. It defaults to true in \class{BaseHandler}, but may have
|
|---|
| 541 | a different default (or be set by the constructor) in the other
|
|---|
| 542 | subclasses.
|
|---|
| 543 | \end{memberdesc}
|
|---|
| 544 |
|
|---|
| 545 | \begin{memberdesc}{wsgi_multiprocess}
|
|---|
| 546 | The value to be used for the \code{wsgi.multiprocess} environment
|
|---|
| 547 | variable. It defaults to true in \class{BaseHandler}, but may have
|
|---|
| 548 | a different default (or be set by the constructor) in the other
|
|---|
| 549 | subclasses.
|
|---|
| 550 | \end{memberdesc}
|
|---|
| 551 |
|
|---|
| 552 | \begin{memberdesc}{wsgi_run_once}
|
|---|
| 553 | The value to be used for the \code{wsgi.run_once} environment
|
|---|
| 554 | variable. It defaults to false in \class{BaseHandler}, but
|
|---|
| 555 | \class{CGIHandler} sets it to true by default.
|
|---|
| 556 | \end{memberdesc}
|
|---|
| 557 |
|
|---|
| 558 | \begin{memberdesc}{os_environ}
|
|---|
| 559 | The default environment variables to be included in every request's
|
|---|
| 560 | WSGI environment. By default, this is a copy of \code{os.environ} at
|
|---|
| 561 | the time that \module{wsgiref.handlers} was imported, but subclasses can
|
|---|
| 562 | either create their own at the class or instance level. Note that the
|
|---|
| 563 | dictionary should be considered read-only, since the default value is
|
|---|
| 564 | shared between multiple classes and instances.
|
|---|
| 565 | \end{memberdesc}
|
|---|
| 566 |
|
|---|
| 567 | \begin{memberdesc}{server_software}
|
|---|
| 568 | If the \member{origin_server} attribute is set, this attribute's value
|
|---|
| 569 | is used to set the default \code{SERVER_SOFTWARE} WSGI environment
|
|---|
| 570 | variable, and also to set a default \code{Server:} header in HTTP
|
|---|
| 571 | responses. It is ignored for handlers (such as \class{BaseCGIHandler}
|
|---|
| 572 | and \class{CGIHandler}) that are not HTTP origin servers.
|
|---|
| 573 | \end{memberdesc}
|
|---|
| 574 |
|
|---|
| 575 |
|
|---|
| 576 |
|
|---|
| 577 | \begin{methoddesc}{get_scheme}{}
|
|---|
| 578 | Return the URL scheme being used for the current request. The default
|
|---|
| 579 | implementation uses the \function{guess_scheme()} function from
|
|---|
| 580 | \module{wsgiref.util} to guess whether the scheme should be ``http'' or
|
|---|
| 581 | ``https'', based on the current request's \member{environ} variables.
|
|---|
| 582 | \end{methoddesc}
|
|---|
| 583 |
|
|---|
| 584 | \begin{methoddesc}{setup_environ}{}
|
|---|
| 585 | Set the \member{environ} attribute to a fully-populated WSGI
|
|---|
| 586 | environment. The default implementation uses all of the above methods
|
|---|
| 587 | and attributes, plus the \method{get_stdin()}, \method{get_stderr()},
|
|---|
| 588 | and \method{add_cgi_vars()} methods and the \member{wsgi_file_wrapper}
|
|---|
| 589 | attribute. It also inserts a \code{SERVER_SOFTWARE} key if not present,
|
|---|
| 590 | as long as the \member{origin_server} attribute is a true value and the
|
|---|
| 591 | \member{server_software} attribute is set.
|
|---|
| 592 | \end{methoddesc}
|
|---|
| 593 |
|
|---|
| 594 |
|
|---|
| 595 |
|
|---|
| 596 |
|
|---|
| 597 |
|
|---|
| 598 |
|
|---|
| 599 |
|
|---|
| 600 |
|
|---|
| 601 |
|
|---|
| 602 |
|
|---|
| 603 |
|
|---|
| 604 |
|
|---|
| 605 |
|
|---|
| 606 |
|
|---|
| 607 |
|
|---|
| 608 |
|
|---|
| 609 |
|
|---|
| 610 |
|
|---|
| 611 |
|
|---|
| 612 |
|
|---|
| 613 |
|
|---|
| 614 |
|
|---|
| 615 |
|
|---|
| 616 |
|
|---|
| 617 |
|
|---|
| 618 | Methods and attributes for customizing exception handling:
|
|---|
| 619 |
|
|---|
| 620 | \begin{methoddesc}{log_exception}{exc_info}
|
|---|
| 621 | Log the \var{exc_info} tuple in the server log. \var{exc_info} is a
|
|---|
| 622 | \code{(\var{type}, \var{value}, \var{traceback})} tuple. The default
|
|---|
| 623 | implementation simply writes the traceback to the request's
|
|---|
| 624 | \code{wsgi.errors} stream and flushes it. Subclasses can override this
|
|---|
| 625 | method to change the format or retarget the output, mail the traceback
|
|---|
| 626 | to an administrator, or whatever other action may be deemed suitable.
|
|---|
| 627 | \end{methoddesc}
|
|---|
| 628 |
|
|---|
| 629 | \begin{memberdesc}{traceback_limit}
|
|---|
| 630 | The maximum number of frames to include in tracebacks output by the
|
|---|
| 631 | default \method{log_exception()} method. If \code{None}, all frames
|
|---|
| 632 | are included.
|
|---|
| 633 | \end{memberdesc}
|
|---|
| 634 |
|
|---|
| 635 | \begin{methoddesc}{error_output}{environ, start_response}
|
|---|
| 636 | This method is a WSGI application to generate an error page for the
|
|---|
| 637 | user. It is only invoked if an error occurs before headers are sent
|
|---|
| 638 | to the client.
|
|---|
| 639 |
|
|---|
| 640 | This method can access the current error information using
|
|---|
| 641 | \code{sys.exc_info()}, and should pass that information to
|
|---|
| 642 | \var{start_response} when calling it (as described in the ``Error
|
|---|
| 643 | Handling'' section of \pep{333}).
|
|---|
| 644 |
|
|---|
| 645 | The default implementation just uses the \member{error_status},
|
|---|
| 646 | \member{error_headers}, and \member{error_body} attributes to generate
|
|---|
| 647 | an output page. Subclasses can override this to produce more dynamic
|
|---|
| 648 | error output.
|
|---|
| 649 |
|
|---|
| 650 | Note, however, that it's not recommended from a security perspective to
|
|---|
| 651 | spit out diagnostics to any old user; ideally, you should have to do
|
|---|
| 652 | something special to enable diagnostic output, which is why the default
|
|---|
| 653 | implementation doesn't include any.
|
|---|
| 654 | \end{methoddesc}
|
|---|
| 655 |
|
|---|
| 656 |
|
|---|
| 657 |
|
|---|
| 658 |
|
|---|
| 659 | \begin{memberdesc}{error_status}
|
|---|
| 660 | The HTTP status used for error responses. This should be a status
|
|---|
| 661 | string as defined in \pep{333}; it defaults to a 500 code and message.
|
|---|
| 662 | \end{memberdesc}
|
|---|
| 663 |
|
|---|
| 664 | \begin{memberdesc}{error_headers}
|
|---|
| 665 | The HTTP headers used for error responses. This should be a list of
|
|---|
| 666 | WSGI response headers (\code{(\var{name}, \var{value})} tuples), as
|
|---|
| 667 | described in \pep{333}. The default list just sets the content type
|
|---|
| 668 | to \code{text/plain}.
|
|---|
| 669 | \end{memberdesc}
|
|---|
| 670 |
|
|---|
| 671 | \begin{memberdesc}{error_body}
|
|---|
| 672 | The error response body. This should be an HTTP response body string.
|
|---|
| 673 | It defaults to the plain text, ``A server error occurred. Please
|
|---|
| 674 | contact the administrator.''
|
|---|
| 675 | \end{memberdesc}
|
|---|
| 676 |
|
|---|
| 677 |
|
|---|
| 678 |
|
|---|
| 679 |
|
|---|
| 680 |
|
|---|
| 681 |
|
|---|
| 682 |
|
|---|
| 683 |
|
|---|
| 684 |
|
|---|
| 685 |
|
|---|
| 686 |
|
|---|
| 687 |
|
|---|
| 688 |
|
|---|
| 689 |
|
|---|
| 690 |
|
|---|
| 691 |
|
|---|
| 692 |
|
|---|
| 693 |
|
|---|
| 694 |
|
|---|
| 695 |
|
|---|
| 696 |
|
|---|
| 697 |
|
|---|
| 698 |
|
|---|
| 699 |
|
|---|
| 700 | Methods and attributes for \pep{333}'s ``Optional Platform-Specific File
|
|---|
| 701 | Handling'' feature:
|
|---|
| 702 |
|
|---|
| 703 | \begin{memberdesc}{wsgi_file_wrapper}
|
|---|
| 704 | A \code{wsgi.file_wrapper} factory, or \code{None}. The default value
|
|---|
| 705 | of this attribute is the \class{FileWrapper} class from
|
|---|
| 706 | \module{wsgiref.util}.
|
|---|
| 707 | \end{memberdesc}
|
|---|
| 708 |
|
|---|
| 709 | \begin{methoddesc}{sendfile}{}
|
|---|
| 710 | Override to implement platform-specific file transmission. This method
|
|---|
| 711 | is called only if the application's return value is an instance of
|
|---|
| 712 | the class specified by the \member{wsgi_file_wrapper} attribute. It
|
|---|
| 713 | should return a true value if it was able to successfully transmit the
|
|---|
| 714 | file, so that the default transmission code will not be executed.
|
|---|
| 715 | The default implementation of this method just returns a false value.
|
|---|
| 716 | \end{methoddesc}
|
|---|
| 717 |
|
|---|
| 718 |
|
|---|
| 719 | Miscellaneous methods and attributes:
|
|---|
| 720 |
|
|---|
| 721 | \begin{memberdesc}{origin_server}
|
|---|
| 722 | This attribute should be set to a true value if the handler's
|
|---|
| 723 | \method{_write()} and \method{_flush()} are being used to communicate
|
|---|
| 724 | directly to the client, rather than via a CGI-like gateway protocol that
|
|---|
| 725 | wants the HTTP status in a special \code{Status:} header.
|
|---|
| 726 |
|
|---|
| 727 | This attribute's default value is true in \class{BaseHandler}, but
|
|---|
| 728 | false in \class{BaseCGIHandler} and \class{CGIHandler}.
|
|---|
| 729 | \end{memberdesc}
|
|---|
| 730 |
|
|---|
| 731 | \begin{memberdesc}{http_version}
|
|---|
| 732 | If \member{origin_server} is true, this string attribute is used to
|
|---|
| 733 | set the HTTP version of the response set to the client. It defaults to
|
|---|
| 734 | \code{"1.0"}.
|
|---|
| 735 | \end{memberdesc}
|
|---|
| 736 |
|
|---|
| 737 |
|
|---|
| 738 |
|
|---|
| 739 |
|
|---|
| 740 |
|
|---|
| 741 | \end{classdesc}
|
|---|
| 742 |
|
|---|
| 743 |
|
|---|
| 744 |
|
|---|
| 745 |
|
|---|
| 746 |
|
|---|
| 747 |
|
|---|
| 748 |
|
|---|
| 749 |
|
|---|
| 750 |
|
|---|
| 751 |
|
|---|
| 752 |
|
|---|
| 753 |
|
|---|
| 754 |
|
|---|
| 755 |
|
|---|
| 756 |
|
|---|
| 757 |
|
|---|
| 758 |
|
|---|
| 759 |
|
|---|
| 760 |
|
|---|
| 761 |
|
|---|
| 762 |
|
|---|
| 763 |
|
|---|
| 764 |
|
|---|
| 765 |
|
|---|
| 766 |
|
|---|
| 767 |
|
|---|
| 768 |
|
|---|
| 769 |
|
|---|
| 770 |
|
|---|
| 771 |
|
|---|
| 772 |
|
|---|
| 773 |
|
|---|
| 774 |
|
|---|
| 775 |
|
|---|
| 776 |
|
|---|
| 777 |
|
|---|
| 778 |
|
|---|
| 779 |
|
|---|
| 780 |
|
|---|
| 781 |
|
|---|