def application(environ, start_response): headers = [('Content-type', 'text/html')] try: path = environ.get('PATH_INFO', None) if path is None: raise NameError func_name, func, args = resolve_path(path) body = Template.answer(func_name, func(*args)) body = func(*args) status = "200 OK" except NameError: status = "404 Not Found" body = '<h1>Not Found</h1>' except Exception: status = '500 Internal Server Error' body = '<h1>Internal Server Error</h1>' print(traceback.format_exc()) finally: headers.append(('Content-length', str(len(body)))) start_response(status, headers) return [body.encode('utf8')]