WebOb
WebOb is a Python library that provides objects for HTTP requests and responses, specifically by wrapping the WSGI request environment and response status/headers/body. It offers many conveniences for parsing HTTP requests and forming HTTP responses, serving as a foundational component for various Python web frameworks. The library is currently at version 1.8.9 and is actively maintained by the Pylons Project, with a consistent release cadence addressing bugs and security fixes.
Warnings
- breaking The `Response.set_cookie` method's `key` parameter was renamed to `name`. Using `key` was deprecated in WebOb 1.5 and completely removed in 1.7.
- breaking Setting a text `body` without explicitly specifying a `charset` in `Response` objects will raise a `TypeError` since WebOb 1.7. Previously, it might have silently defaulted.
- breaking The `status` attribute of a `Response` object no longer accepts arbitrary strings (like `None None`) and now strictly requires a format matching `<integer status code> <explanation of status code>`. Invalid strings will raise a `ValueError`.
- breaking WebOb 1.8.0 introduced significant changes to Accept header handling (Accept, Accept-Charset, Accept-Encoding, Accept-Language), potentially breaking applications relying on previous parsing behaviors.
- security A security vulnerability (CVE-2024-42353) in WebOb 1.8.8 and earlier can lead to an open redirect if `Response` objects are used to redirect to an unvalidated `Location` header, which is not a full URI.
- gotcha The `SameSite` cookie attribute's 'None' value was introduced in WebOb 1.8.6. While WebOb doesn't enable `SameSite` by default, older clients may be incompatible with this new value, leading to unexpected cookie behavior.
Install
-
pip install webob
Imports
- Request
from webob import Request
- Response
from webob import Response
- HTTPNotFound
from webob.exc import HTTPNotFound
Quickstart
from webob import Request, Response
def application(environ, start_response):
request = Request(environ)
response = Response()
if request.path == '/':
response.status = '200 OK'
response.content_type = 'text/html'
response.text = '<h1>Hello, WebOb!</h1>'
else:
response.status = '404 Not Found'
response.content_type = 'text/plain'
response.text = 'Not Found'
return response(environ, start_response)
# Example of how to 'run' a request for testing (not a full WSGI server)
if __name__ == '__main__':
from wsgiref.simple_server import make_server
httpd = make_server('', 8000, application)
print('Serving on http://localhost:8000')
httpd.serve_forever()