Skip to content

Instantly share code, notes, and snippets.

@Averroes
Created April 10, 2015 17:04
Show Gist options
  • Select an option

  • Save Averroes/a8916c27b71e808b55de to your computer and use it in GitHub Desktop.

Select an option

Save Averroes/a8916c27b71e808b55de to your computer and use it in GitHub Desktop.
creating a simple rest based interface
# resty.py
import cgi
def notfound_404(environ, start_response):
start_response('404 Not Found', [ ('Content-type', 'text/plain') ])
return [b'Not Found']
class PathDispatcher:
def __init__(self):
self.pathmap = { }
def __call__(self, environ, start_response):
path = environ['PATH_INFO']
params = cgi.FieldStorage(environ['wsgi.input'],
environ=environ)
method = environ['REQUEST_METHOD'].lower()
environ['params'] = { key: params.getvalue(key) for key in params }
handler = self.pathmap.get((method,path), notfound_404)
return handler(environ, start_response)
def register(self, method, path, function):
self.pathmap[method.lower(), path] = function
return function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment