def do_GET(self): res = 404 error = "Not found" body = None components = self.path.split("/") while components and not components[0].strip(): components = components[1:] while components: name = components[0].strip() call_path = "/".join(components[1:]) try: instance = ServiceRequestHandler.services[name] except KeyError: error = "Not found: %r" % name break try: command, args = url_path_to_call(call_path) except Exception: res = 400 error = "Bad Request: Failed to parse operation" break try: method = getattr(instance, command) except AttributeError: error = "Not found: %s :: %r" % (name, command) break try: output = True, method(*args) except Exception as exc: output = False, pack_exception(exc) res = 200 try: body = serialize(output) error = None except Exception as exc: print "Returning", output print "Failed because:", exc raise break if error: self.send_error(res, error) return self.send_response(res) self.end_headers() if body: self.wfile.write(body)
def call_to_url_path(name, args): return "/" + serialize((name, args)).encode("base64").replace("\n", "")