/examples/web/filtering.py

https://bitbucket.org/prologic/circuits/ · Python · 48 lines · 27 code · 4 blank · 17 comment · 1 complexity · 2edee001b103aa0322284f697ddf98e7 MD5 · raw file

  1. #!/usr/bin/env python
  2. """Filtering
  3. A simple example showing how to intercept and potentially filter requests.
  4. This example demonstrates how you could intercept the response before it goes
  5. out changing the response's content into ALL UPPER CASE!
  6. """
  7. from circuits import handler, Component
  8. from circuits.web import Server, Controller
  9. class Upper(Component):
  10. channel = "web" # By default all web related events
  11. # go to the "web" channel.
  12. @handler("response", priority=1.0)
  13. def _on_response(self, response):
  14. """Response Handler
  15. Here we set the priority slightly higher than the default response
  16. handler in circutis.web (0.0) so that can we do something about the
  17. response before it finally goes out to the client.
  18. Here's we're simply modifying the response body by changing the content
  19. into ALL UPPERCASE!
  20. """
  21. response.body = "".join(response.body).upper()
  22. class Root(Controller):
  23. def index(self):
  24. """Request Handler
  25. Our normal request handler simply returning
  26. "Hello World!" as the response.
  27. """
  28. return "Hello World!"
  29. app = Server(("0.0.0.0", 8000))
  30. Upper().register(app)
  31. Root().register(app)
  32. app.run()