Python wsgiref Module
Example
Create a simple WSGI application:
from wsgiref.simple_server import make_server
def application(environ, start_response):
status = '200 OK'
headers = [('Content-type', 'text/plain; charset=utf-8')]
start_response(status, headers)
return [b'Hello from WSGI!']
with make_server('', 8000, application) as httpd:
print('Serving on port 8000...')
httpd.handle_request()
Try it Yourself »
Definition and Usage
The wsgiref module provides utilities and a reference implementation of the WSGI (Web Server Gateway Interface) specification.
Use it to build web applications, create simple web servers for testing, or implement WSGI middleware and utilities.
Note: The simple_server component is intended for development and testing, not for production use. Use a production-ready WSGI server like Gunicorn or uWSGI for deployment.
Members
Member | Description |
---|---|
handlers.BaseHandler | Parent class for creating WSGI server handlers. |
handlers.CGIHandler | Handler for running WSGI apps as CGI scripts. |
handlers.IISCGIHandler | Handler specialized for Microsoft IIS web server. |
handlers.SimpleHandler | Basic handler that works with any file-like input/output. |
headers.Headers | Manage HTTP response headers easily. |
simple_server.WSGIRequestHandler | Handles individual HTTP requests for the WSGI server. |
simple_server.WSGIServer | A simple HTTP server that runs WSGI applications. |
simple_server.demo_app() | A tiny example WSGI app for testing. |
simple_server.make_server() | Create a WSGI server quickly with just host, port, and app. |
util.FileWrapper | Wrap a file so it can be sent efficiently as WSGI output. |
util.application_uri() | Build the base URL of the application without the query string. |
util.guess_scheme() | Figure out if the request is 'http' or 'https'. |
util.is_hop_by_hop() | Check if an HTTP header should not be forwarded to the client. |
util.request_uri() | Build the complete request URL including query string. |
util.setup_testing_defaults() | Fill in a WSGI environment dict with default testing values. |
util.shift_path_info() | Move one path segment from PATH_INFO to SCRIPT_NAME for routing. |
validate.validator() | Wrap a WSGI app to check if it follows WSGI rules correctly. |