def notify(self, method, *args, **kwargs): """ Sends a notification or multiple notifications to the server. A notification is a special request which does not have a response. """ methods = [] # Create JSON-RPC-request if isinstance(method, basestring): request_dict = rpcrequest.create_request_dict(method, *args, **kwargs) request_dict["id"] = None methods.append(request_dict) else: assert not args and not kwargs for request_dict in method: request_dict["id"] = None methods.append(request_dict) # Redirect to call-method self.call(methods) # Fertig return
def notify(self, method, *args, **kwargs): """ Sends a notification or multiple notifications to the server. A notification is a special request which does not have a response. """ methods = [] # Create JSON-RPC-request (without ID) if isinstance(method, basestring): request_dict = rpcrequest.create_request_dict(method, *args, **kwargs) del request_dict["id"] methods.append(request_dict) else: assert not args and not kwargs for request_dict in method: for delkey in ["id", "ID", "Id"]: if delkey in request_dict: del request_dict[delkey] methods.append(request_dict) # Redirect to call-method self.call(methods) # Fertig return
def notify(self, method, *args, **kwargs): """ Sends a notification or multiple notifications to the server. A notification is a special request which does not have a response. """ methods = [] # Create JSON-RPC-request (without ID) if isinstance(method, string_types): request_dict = rpcrequest.create_request_dict(method, *args, **kwargs) del request_dict["id"] methods.append(request_dict) else: assert not args and not kwargs for request_dict in method: for delkey in ["id", "ID", "Id"]: if delkey in request_dict: del request_dict[delkey] methods.append(request_dict) # Redirect to call-method self.call(methods) # Fertig return
def request_handler(self, *args, **kwargs): """ Json-RPC Handler """ if cherrypy.request.method == "GET": # GET # Arguments jsonrpc = kwargs.get("jsonrpc") id = kwargs.get("id") method = kwargs.get("method") if not method: # Bad Request raise cherrypy.HTTPError(httplib.BAD_REQUEST) # params _args = [] _kwargs = {} params = kwargs.get("params") if params: params = rpcjson.loads(params) if isinstance(params, list): _args = params _kwargs = {} elif isinstance(params, dict): _args = [] _kwargs = params # Create JSON request string request_dict = rpcrequest.create_request_dict( method, *_args, **_kwargs) request_dict["jsonrpc"] = jsonrpc request_dict["id"] = id request_json = rpcjson.dumps(request_dict) else: # POST if "gzip" in cherrypy.request.headers.get("Content-Encoding", ""): request_json = decompress(cherrypy.request.body.read()) else: request_json = cherrypy.request.body.read() # Call method result_string = self.call(request_json) or "" # Return JSON-String cherrypy.response.headers["Cache-Control"] = "no-cache" cherrypy.response.headers["Pragma"] = "no-cache" cherrypy.response.headers["Content-Type"] = "application/json" if "gzip" in cherrypy.request.headers.get("Accept-Encoding", ""): # Gzip-compressed cherrypy.response.headers["Content-Encoding"] = "gzip" return compress(result_string, compress_level=5) else: # uncompressed return result_string
def request_handler(self, *args, **kwargs): """ Json-RPC Handler """ if cherrypy.request.method == "GET": # GET # Arguments jsonrpc = kwargs.get("jsonrpc") id = kwargs.get("id") method = kwargs.get("method") if not method: # Bad Request raise cherrypy.HTTPError(httplib.BAD_REQUEST) # params _args = [] _kwargs = {} params = kwargs.get("params") if params: params = rpcjson.loads(params) if isinstance(params, list): _args = params _kwargs = {} elif isinstance(params, dict): _args = [] _kwargs = params # Create JSON request string request_dict = rpcrequest.create_request_dict(method, *_args, **_kwargs) request_dict["jsonrpc"] = jsonrpc request_dict["id"] = id request_json = rpcjson.dumps(request_dict) else: # POST if "gzip" in cherrypy.request.headers.get("Content-Encoding", ""): request_json = decompress(cherrypy.request.body.read()) else: request_json = cherrypy.request.body.read() # Call method result_string = self.call(request_json) or "" # Return JSON-String cherrypy.response.headers["Cache-Control"] = "no-cache" cherrypy.response.headers["Pragma"] = "no-cache" cherrypy.response.headers["Content-Type"] = "application/json" if "gzip" in cherrypy.request.headers.get("Accept-Encoding", ""): # Gzip-compressed cherrypy.response.headers["Content-Encoding"] = "gzip" return compress(result_string, compress_level = 5) else: # uncompressed return result_string
def do_GET(self): """ Handles HTTP-GET-Request """ # Parse URL query query = urlparse.parse_qs(urllib.splitquery(self.path)[1]) # jsonrpc jsonrpc = query.get("jsonrpc") if jsonrpc: jsonrpc = jsonrpc[0] # id id = query.get("id") if id: id = id[0] # method method = query.get("method") if method: method = method[0] # params args = [] kwargs = {} params = query.get("params") if params: params = json.loads(params[0]) if isinstance(params, list): args = params kwargs = {} elif isinstance(params, dict): args = [] kwargs = params # Create JSON reqeust string request_dict = rpcrequest.create_request_dict(method, *args, **kwargs) request_dict["jsonrpc"] = jsonrpc request_dict["id"] = id request_json = json.dumps(request_dict) # Call response_json = self.call(request_json) # Return result self.send_response(code = httplib.OK) self.set_content_type_json() self.set_no_cache() self.set_content_length(len(response_json)) self.end_headers() self.wfile.write(response_json)
def do_GET(self): """ Handles HTTP-GET-Request """ # Parse URL query query = urlparse.parse_qs(urllib.splitquery(self.path)[1]) # jsonrpc jsonrpc = query.get("jsonrpc") if jsonrpc: jsonrpc = jsonrpc[0] # id id = query.get("id") if id: id = id[0] # method method = query.get("method") if method: method = method[0] # params args = [] kwargs = {} params = query.get("params") if params: params = json.loads(params[0]) if isinstance(params, list): args = params kwargs = {} elif isinstance(params, dict): args = [] kwargs = params # Create JSON reqeust string request_dict = rpcrequest.create_request_dict(method, *args, **kwargs) request_dict["jsonrpc"] = jsonrpc request_dict["id"] = id request_json = json.dumps(request_dict) # Call response_json = self.call(request_json) # Return result self.send_response(code=httplib.OK) self.set_content_type_json() self.set_no_cache() self.set_content_length(len(response_json)) self.end_headers() self.wfile.write(response_json)
def handle_cgi_request(methods = None): """ Gets the JSON-RPC request from CGI environment and returns the result to STDOUT """ import cgi import cgitb cgitb.enable() # get response-body request_json = sys.stdin.read() if request_json: # POST request_json = urlparse.unquote(request_json) else: # GET args = [] kwargs = {} fields = cgi.FieldStorage() jsonrpc = fields.getfirst("jsonrpc") id = fields.getfirst("id") method = fields.getfirst("method") params = fields.getfirst("params") if params: params = rpcjson.loads(params) if isinstance(params, list): args = params kwargs = {} elif isinstance(params, dict): args = [] kwargs = params # Create JSON request string request_dict = rpcrequest.create_request_dict(method, *args, **kwargs) request_dict["jsonrpc"] = jsonrpc request_dict["id"] = id request_json = rpcjson.dumps(request_dict) # Call response_json = rpclib.JsonRpc(methods = methods).call(request_json) # Return headers print("Content-Type: application/json") print("Cache-Control: no-cache") print("Pragma: no-cache") print() # Return result print(response_json)
def handle_cgi_request(methods = None): """ Gets the JSON-RPC request from CGI environment and returns the result to STDOUT """ import cgi import cgitb cgitb.enable() # get response-body request_json = sys.stdin.read() if request_json: # POST request_json = urlparse.unquote(request_json) else: # GET args = [] kwargs = {} fields = cgi.FieldStorage() jsonrpc = fields.getfirst("jsonrpc") id = fields.getfirst("id") method = fields.getfirst("method") params = fields.getfirst("params") if params: params = rpcjson.loads(params) if isinstance(params, list): args = params kwargs = {} elif isinstance(params, dict): args = [] kwargs = params # Create JSON request string request_dict = rpcrequest.create_request_dict(method, *args, **kwargs) request_dict["jsonrpc"] = jsonrpc request_dict["id"] = id request_json = rpcjson.dumps(request_dict) # Call response_json = rpclib.JsonRpc(methods = methods).call(request_json) # Return headers print "Content-Type: application/json" print "Cache-Control: no-cache" print "Pragma: no-cache" print # Return result print response_json
def do_GET(self): """ Handles HTTP-GET-Request """ # Parse URL query path, query_str = urllib.splitquery(self.path) if six.PY2 else urlparse.splitquery(self.path) if not query_str: # Bad Request return self.send_error(httplib.BAD_REQUEST) # Parse querystring query = urlparse.parse_qs(query_str) # jsonrpc jsonrpc = query.get("jsonrpc") if jsonrpc: jsonrpc = jsonrpc[0] # id id = query.get("id") if id: id = id[0] # method method = query.get("method") if method: method = method[0] else: # Bad Request return self.send_error(httplib.BAD_REQUEST) # params args = [] kwargs = {} params = query.get("params") if params: params = rpcjson.loads(params[0]) if isinstance(params, list): args = params kwargs = {} elif isinstance(params, dict): args = [] kwargs = params # Create JSON request string request_dict = rpcrequest.create_request_dict(method, *args, **kwargs) request_dict["jsonrpc"] = jsonrpc request_dict["id"] = id request_json = rpcjson.dumps(request_dict) # Call response_json = self.call(request_json) or "" # Return result self.send_response(code=httplib.OK) self.set_content_type(self.content_type) self.set_no_cache() self.set_content_length(len(response_json)) self.end_headers() self.wfile.write(response_json)