/examples/web/jsonserializer.py

https://bitbucket.org/prologic/circuits/ · Python · 28 lines · 17 code · 10 blank · 1 comment · 0 complexity · 56dbcd1eee59481eeeb8cff1de0b2691 MD5 · raw file

  1. #!/usr/bin/env python
  2. from json import dumps
  3. from circuits import handler, Component
  4. from circuits.web import Server, Controller, Logger
  5. class JSONSerializer(Component):
  6. channel = "web"
  7. @handler("response", priority=1.0) # 1 higher than the default response handler
  8. def serialize_response_body(self, response):
  9. response.headers["Content-Type"] = "application/json"
  10. response.body = dumps(response.body)
  11. class Root(Controller):
  12. def index(self):
  13. return {"message": "Hello World!"}
  14. app = Server(("0.0.0.0", 9000))
  15. JSONSerializer().register(app)
  16. Logger().register(app)
  17. Root().register(app)
  18. app.run()