Skip to content

Instantly share code, notes, and snippets.

@pckujawa
Forked from alex/webapp.py
Created March 29, 2012 02:39
Show Gist options
  • Save pckujawa/2232688 to your computer and use it in GitHub Desktop.
Save pckujawa/2232688 to your computer and use it in GitHub Desktop.
turn any python object into a web app
import traceback
class WebApp(object):
def __init__(self, obj):
self.obj = obj
def __call__(self, environ, start_response):
try:
path = filter(bool, environ["PATH_INFO"].split("/"))
try:
method = path.pop(0)
except IndexError:
res = self.obj
else:
res = getattr(self.obj, method)(*path)
start_response("200 OK", {})
return [str(res)]
except Exception:
start_response("500 INTERNAL SERVER ERROR", {})
return [traceback.format_exc()]
application = WebApp([])
@pckujawa
Copy link
Author

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