PageRenderTime 8ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 3from json import dumps
 4
 5from circuits import handler, Component
 6from circuits.web import Server, Controller, Logger
 7
 8
 9class JSONSerializer(Component):
10
11    channel = "web"
12
13    @handler("response", priority=1.0)  # 1 higher than the default response handler
14    def serialize_response_body(self, response):
15        response.headers["Content-Type"] = "application/json"
16        response.body = dumps(response.body)
17
18
19class Root(Controller):
20
21    def index(self):
22        return {"message": "Hello World!"}
23
24app = Server(("0.0.0.0", 9000))
25JSONSerializer().register(app)
26Logger().register(app)
27Root().register(app)
28app.run()