コード例 #1
0
ファイル: cp.py プロジェクト: pombreda/telenium
    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
コード例 #2
0
ファイル: cp.py プロジェクト: chris-barry/i2py
    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
コード例 #3
0
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)
コード例 #4
0
ファイル: http.py プロジェクト: scls19fr/python-jsonrpc
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
コード例 #5
0
    def from_string(cls, json_string):
        """
        Returns a Response-object or a list with Response-objects
        """

        if not json_string:
            return

        data = rpcjson.loads(json_string)

        if isinstance(data, list):
            retlist = []
            for response in data:
                retlist.append(cls.from_dict(response))
            return retlist
        else:
            return cls.from_dict(data)
コード例 #6
0
ファイル: rpcresponse.py プロジェクト: chris-barry/i2py
    def from_string(cls, json_string):
        """
        Returns a Response-object or a list with Response-objects
        """

        if not json_string:
            return

        data = rpcjson.loads(json_string)

        if isinstance(data, list):
            retlist = []
            for response in data:
                retlist.append(cls.from_dict(response))
            return retlist
        else:
            return cls.from_dict(data)
コード例 #7
0
ファイル: rpcrequest.py プロジェクト: karbon62/python-jsonrpc
    def from_string(cls, json_string):
        """
        Parses the Json-string and returns a Request-object or a
        list with Request-objects.

        :returns: Request-object or list with Request-objects

        :rtype: Request
        """

        # No JSON-String
        if not json_string:
            raise rpcerror.InvalidRequest()

        # Parse
        try:
            data = rpcjson.loads(json_string)
        except rpcjson.JsonParseError:
            traceback_info = "".join(traceback.format_exception(*sys.exc_info()))
            raise rpcerror.ParseError(data = traceback_info)

        # Create request(s)
        if isinstance(data, list):
            requests = []
            for item in data:
                requests.append(cls(
                    jsonrpc = item.get("jsonrpc"),
                    method = item.get("method"),
                    id = item.get("id"),
                    params = item.get("params")
                ))
            return requests
        else:
            return cls(
                jsonrpc = data.get("jsonrpc"),
                method = data.get("method"),
                id = data.get("id"),
                params = data.get("params")
            )
コード例 #8
0
    def from_string(cls, json_string):
        """
        Parses the Json-string and returns a Request-object or a
        list with Request-objects.

        :returns: Request-object or list with Request-objects

        :rtype: Request
        """

        # No JSON-String
        if not json_string:
            raise rpcerror.InvalidRequest()

        # Parse
        try:
            data = rpcjson.loads(json_string)
        except rpcjson.JsonParseError:
            traceback_info = "".join(traceback.format_exception(*sys.exc_info()))
            raise rpcerror.ParseError(data = traceback_info)

        # Create request(s)
        if isinstance(data, list):
            requests = []
            for item in data:
                requests.append(cls(
                    jsonrpc = item.get("jsonrpc"),
                    method = item.get("method"),
                    id = item.get("id"),
                    params = item.get("params")
                ))
            return requests
        else:
            return cls(
                jsonrpc = data.get("jsonrpc"),
                method = data.get("method"),
                id = data.get("id"),
                params = data.get("params")
            )
コード例 #9
0
ファイル: http.py プロジェクト: scls19fr/python-jsonrpc
    def do_GET(self):
        """
        Handles HTTP-GET-Request
        """

        # Parse URL query
        path, query_str = urllib.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)
コード例 #10
0
    def do_GET(self):
        """
        Handles HTTP-GET-Request
        """

        # Parse URL query
        path, query_str = urllib.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)