/examples/web/acldemo.py

https://bitbucket.org/prologic/circuits/ · Python · 35 lines · 17 code · 10 blank · 8 comment · 1 complexity · c469d37e3fa33cbf808ca92ca6c80312 MD5 · raw file

  1. #!/usr/bin/env python
  2. from circuits import handler, Component
  3. from circuits.web.errors import Forbidden
  4. from circuits.web import Server, Controller
  5. class ACL(Component):
  6. allowed = ["127.0.0.1"]
  7. @handler("request", priority=1.0)
  8. def on_request(self, event, request, response):
  9. """Filter Requests applying IP based Authorization
  10. Filter any incoming requests at a higher priority than the
  11. default dispatcher and apply IP based Authorization returning
  12. a 403 Forbidden response if the Remote IP Address does not
  13. match the allowed set.
  14. """
  15. if not request.remote.ip in self.allowed:
  16. event.stop()
  17. return Forbidden(request, response)
  18. class Root(Controller):
  19. def index(self):
  20. return "Hello World!"
  21. app = Server(("0.0.0.0", 8000))
  22. ACL().register(app)
  23. Root().register(app)
  24. app.run()