Blog 2019/3/8
<- previous | index | next ->
A trivial WSGI app which tries to make WSGI a bit more like Clojure's Ring framework (dictionary-in, dictionary-out).
| def root_endpoint(request): | |
| d = {} | |
| d["status"] = '200 OK' | |
| d["body"] = 'Root!\n' | |
| d["headers"] = [ | |
| ('Content-type', 'text/plain'), | |
| ('Content-Length', str(len(d["body"]))) | |
| ] | |
| return d | |
| def hello_endpoint(request): | |
| d = {} | |
| d["status"] = '200 OK' | |
| d["body"] = 'Hello, world!\n' | |
| d["headers"] = [ | |
| ('Content-type', 'text/plain'), | |
| ('Content-Length', str(len(d["body"]))) | |
| ] | |
| return d | |
| routes = { | |
| "/": root_endpoint, | |
| "/hello": hello_endpoint | |
| } | |
| def route(request): | |
| return routes[request["PATH_INFO"]] | |
| def application(request, start_response_fn): | |
| handler = route(request) | |
| response = handler(request) | |
| start_response_fn(response["status"], response["headers"]) | |
| return [response["body"].encode()] |
| sudo pip install mod_wsgi | |
| mod_wsgi-express start-server ring.wsgi |
Note: for python2, the last line of the above example was originally: