Created
April 10, 2015 17:04
-
-
Save Averroes/a8916c27b71e808b55de to your computer and use it in GitHub Desktop.
creating a simple rest based interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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