Skip to content

Instantly share code, notes, and snippets.

Created July 25, 2014 21:06
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
from werkzeug import wrappers
def application(request):
request > get("/") > do_response()
request > get("/msg") > say_hello()
# 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):
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,
)(environ, start_response)
return HelloResponder()
if __name__ == '__main__':
from werkzeug.serving import run_simple
def app(environ, start_response):
request = wrappers.Request(environ)
except HttpResponse as response:
return, start_response)
run_simple("", 1981, app, use_reloader=True)
Copy link

This is in a better form here

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