from webob import Request, Response def application(request, response): if request.method != 'GET': response.status_code = 405 return response response.text = 'Hello World' return response req = Request.blank('/') res = req.get_response(application) print(res.text)
from webob import Request, Response def application(request, response): if request.path == '/about': response.text = 'This is the About page' elif request.path == '/contact': response.text = 'This is the Contact page' else: response.status_code = 404 response.text = 'Page not found' return response req1 = Request.blank('/about') res1 = req1.get_response(application) print(res1.text) req2 = Request.blank('/contact') res2 = req2.get_response(application) print(res2.text) req3 = Request.blank('/foo') res3 = req3.get_response(application) print(res3.status_code)In this example, we define a more complex controller function `application()` that returns different content for different URL paths. We create three `Request` objects with different URL paths and call the `get_response()` method with our controller function to get the responses. We print out the text of the responses for the first two requests, and the status code for the third (which should be 404). In conclusion, Python webob is a package library that provides a simple yet powerful way to handle HTTP requests and responses, and the `Request` class's `get_response()` method plays a central role in generating responses based on requests.