wsgiref
raw JSON → 0.1.2 verified Fri May 01 auth: no python deprecated
wsgiref is the WSGI (PEP 333) reference implementation, included in the Python standard library. It provides utilities for developing and testing WSGI applications, including a simple HTTP server and request/response objects. Version 0.1.2 on PyPI is outdated; the standard library version is bundled with Python 3.x. Use the standard library module instead of the PyPI package to avoid conflicts.
pip install wsgiref Common errors
error ImportError: No module named wsgiref ↓
cause Using an outdated Python version (prior to 2.5) where wsgiref was not in the standard library.
fix
Upgrade Python to 2.5 or later, or use the PyPI package (not recommended).
error AttributeError: module 'wsgiref' has no attribute 'simple_server' ↓
cause Trying to import wsgiref.simple_server when the PyPI package is installed and shadows the standard library, which may have different structure.
fix
Uninstall the PyPI package: pip uninstall wsgiref. Then use the standard library module.
Warnings
deprecated The PyPI package 'wsgiref' is outdated and should not be used. Instead, import from the Python standard library's wsgiref module, which is bundled with Python 3.x. ↓
fix Remove wsgiref from requirements.txt. Use: from wsgiref.simple_server import make_server
gotcha wsgiref is for development and testing only; not suitable for production. Traps SIGINT, so pressing Ctrl+C may not stop the server gracefully on some platforms. ↓
fix For production, use a production-grade server like Gunicorn or uWSGI.
Imports
- simple_server wrong
from wsgiref.simple_server import make_servercorrectfrom wsgiref import simple_server - util wrong
import wsgiref.utilcorrectfrom wsgiref import util
Quickstart
from wsgiref.simple_server import make_server
def hello_app(environ, start_response):
status = '200 OK'
headers = [('Content-type', 'text/plain')]
start_response(status, headers)
return [b'Hello World']
with make_server('', 8000, hello_app) as httpd:
print('Serving on port 8000...')
httpd.serve_forever()