Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
from werkzeug import wrappers
def application(request):
request > get("/") > do_response()
request > get("/msg") > say_hello()
# # WARNING
#
# Don't judge me. :-)
#
# Seriously, the code below this is just proof of concept code that
# enables the application function to actually run.
class HttpResponse(BaseException):
pass
class Http405(HttpResponse):
def do(self, environ, start_response):
return wrappers.Response(status=405)(environ, start_response)
class Http404(HttpResponse):
def do(self, environ, start_response):
return wrappers.Response(status=404)(environ, start_response)
def route(path, methods="GET"):
if not type(methods) is list:
methods = [methods, ]
class Route(object):
def __lt__(self, request):
if not request.path == path:
return False
if not request.method in methods:
raise Http405
request.route = self
self.request = request
return True
return Route()
def get(path):
return route(path, methods=["GET"])
class Response(HttpResponse):
def __lt__(self, route):
self.request = route.request
raise self
def do(self, environ, start_response):
return wrappers.Response("Hello!\n", mimetype="text/html")(environ, start_response)
def do_response():
return Response()
def say_hello():
class HelloResponder(Response):
def do(self, environ, start_response):
who = self.request.args.get('who', 'World')
return wrappers.Response(
"Hello, %s!\n" % who,
mimetype="text/html"
)(environ, start_response)
return HelloResponder()
if __name__ == '__main__':
from werkzeug.serving import run_simple
def app(environ, start_response):
request = wrappers.Request(environ)
try:
application(request)
except HttpResponse as response:
return response.do(environ, start_response)
run_simple("127.0.0.1", 1981, app, use_reloader=True)
Owner

tswicegood commented Jul 26, 2014

This is in a better form here https://github.com/tswicegood/steinie

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