WebOb

1.8.9 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates a minimal WSGI application using WebOb. It handles incoming requests, creates a Response object, and serves a simple 'Hello, WebOb!' page for the root path or a 'Not Found' error for other paths. The example includes a basic `wsgiref` server for local execution.

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()

view raw JSON →