/examples/web/filtering.py
https://bitbucket.org/prologic/circuits/ · Python · 48 lines · 27 code · 4 blank · 17 comment · 1 complexity · 2edee001b103aa0322284f697ddf98e7 MD5 · raw file
- #!/usr/bin/env python
- """Filtering
- A simple example showing how to intercept and potentially filter requests.
- This example demonstrates how you could intercept the response before it goes
- out changing the response's content into ALL UPPER CASE!
- """
- from circuits import handler, Component
- from circuits.web import Server, Controller
- class Upper(Component):
- channel = "web" # By default all web related events
- # go to the "web" channel.
- @handler("response", priority=1.0)
- def _on_response(self, response):
- """Response Handler
- Here we set the priority slightly higher than the default response
- handler in circutis.web (0.0) so that can we do something about the
- response before it finally goes out to the client.
- Here's we're simply modifying the response body by changing the content
- into ALL UPPERCASE!
- """
- response.body = "".join(response.body).upper()
- class Root(Controller):
- def index(self):
- """Request Handler
- Our normal request handler simply returning
- "Hello World!" as the response.
- """
- return "Hello World!"
- app = Server(("0.0.0.0", 8000))
- Upper().register(app)
- Root().register(app)
- app.run()