app.py 794 B

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