Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# -*- coding: utf-8 -*-
Adapted from Armin Ronacher's "webpylike".
With this, an application can look like this:
from webzeug import App, Request, Response, View
class Index(View):
def GET(self):
return Response('Hello World')
class Hello(View):
def GET(self, name):
return Response('Hey there ' + name + '!')
app = App([
('/', 'index', Index),
('/people/<name>/', 'hello', Hello),
if __name__ == "__main__":
from werkzeug.serving import run_simple
run_simple('', 8000, app)
from werkzeug.routing import Map, Rule
from werkzeug.wrappers import BaseRequest, BaseResponse
from werkzeug.exceptions import HTTPException, MethodNotAllowed, \
NotImplemented, NotFound
class Request(BaseRequest):
"""Encapsulates a request."""
class Response(BaseResponse):
"""Encapsulates a response."""
class View(object):
"""Baseclass for our views."""
def __init__(self, app, req): = app
self.req = req
def GET(self):
raise MethodNotAllowed()
def HEAD(self):
return self.GET()
class App(object):
def __init__(self, urls):
self.urls = urls
self.handlers = {}
self.rules = []
for pat, name, func in urls:
self.rules.append(Rule(pat, endpoint=name))
self.handlers[name] = func = Map(self.rules)
def __call__(self, environ, start_response):
req = Request(environ)
adapter =
view_name, params = adapter.match()
view_cls = self.handlers[view_name]
view = view_cls(self, req)
if req.method not in ('GET', 'HEAD', 'POST',
raise NotImplemented()
resp = getattr(view, req.method)(**params)
except HTTPException, e:
resp = e
return resp(environ, start_response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment