Skip to content

Instantly share code, notes, and snippets.

@cellularmitosis
Last active March 4, 2023 06:51
Show Gist options
  • Save cellularmitosis/002c23033cf89aa977e9f6fb9d273afc to your computer and use it in GitHub Desktop.
Save cellularmitosis/002c23033cf89aa977e9f6fb9d273afc to your computer and use it in GitHub Desktop.
Dictionary-in, Dictionary-out style with WSGI

Blog 2019/3/8

<- previous | index | next ->

Dictionary-in, Dictionary-out style with WSGI

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
@cellularmitosis
Copy link
Author

cellularmitosis commented Mar 4, 2023

Note: for python2, the last line of the above example was originally:

    return [response["body"]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment