app.py 712 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/env python
  2. import json
  3. import falcon
  4. # resource endpoints
  5. class JSONResource(object):
  6. def on_get(self, request, response):
  7. json_data = {'message': "Hello, world!"}
  8. response.body = json.dumps(json_data)
  9. class PlaintextResource(object):
  10. def on_get(self, request, response):
  11. response.set_header('Content-Type', 'text/plain')
  12. response.body = b'Hello, world!'
  13. # setup
  14. app = falcon.API()
  15. app.add_route("/json", JSONResource())
  16. app.add_route("/plaintext", PlaintextResource())
  17. # entry point for debugging
  18. if __name__ == "__main__":
  19. from wsgiref import simple_server
  20. httpd = simple_server.make_server('localhost', 8080, app)
  21. httpd.serve_forever()