User Tools

Site Tools


tanszek:oktatas:iss_t:json-rpc

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tanszek:oktatas:iss_t:json-rpc [2026/03/01 17:06] – [JSON-RPC Server (Python + Flask)] kneheztanszek:oktatas:iss_t:json-rpc [2026/03/01 17:27] (current) – [JSON-RPC Client (Python)] knehez
Line 24: Line 24:
 A JSON-RPC request is a JSON object with the following fields: A JSON-RPC request is a JSON object with the following fields:
  
-jsonrpc – must be "2.0" +  * **jsonrpc** – must be "2.0" 
- +  * **method** – name of the remote method 
-method – name of the remote method +  * **params** – parameters (optional) 
- +  * **id** – request identifier (used to match response)
-params – parameters (optional) +
- +
-id – request identifier (used to match response)+
  
 Example request: Example request:
Line 78: Line 75:
 Create a file: //server.py// Create a file: //server.py//
  
-<sxh python> from flask import Flask, request, jsonify+<sxh python>  
 +from flask import Flask, request, jsonify
  
-app = Flask(name)+app = Flask(__name__)
  
-Available RPC methods+# available RPC methods
  
 def add(a, b): def add(a, b):
-return a + b+    return a + b 
  
 def subtract(a, b): def subtract(a, b):
-return a - b+    return a - b 
  
 methods = { methods = {
-"add": add, +    "add": add, 
-"subtract": subtract+    "subtract": subtract,
 } }
 +
  
 @app.route("/rpc", methods=["POST"]) @app.route("/rpc", methods=["POST"])
 def rpc(): def rpc():
-data = request.get_json()+    data = request.get_json()
  
-if data.get("jsonrpc") != "2.0": +    if data.get("jsonrpc") != "2.0": 
-    return jsonify({ +        return jsonify( 
-        "jsonrpc": "2.0", +            
-        "error": {"code": -32600, "message": "Invalid Request"}, +                "jsonrpc": "2.0", 
-        "id": data.get("id"+                "error": {"code": -32600, "message": "Invalid Request"}, 
-    })+                "id": data.get("id"), 
 +            } 
 +        )
  
-method_name = data.get("method"+    method_name = data.get("method"
-params = data.get("params", []) +    params = data.get("params", []) 
-request_id = data.get("id")+    request_id = data.get("id")
  
-if method_name not in methods: +    if method_name not in methods: 
-    return jsonify({ +        return jsonify( 
-        "jsonrpc": "2.0", +            
-        "error": {"code": -32601, "message": "Method not found"}, +                "jsonrpc": "2.0", 
-        "id": request_id +                "error": {"code": -32601, "message": "Method not found"}, 
-    })+                "id": request_id, 
 +            } 
 +        )
  
-try: +    try: 
-    result = methods[method_name](*params) +        result = methods[method_name](*params) 
-    return jsonify({ +        return jsonify( 
-        "jsonrpc": "2.0", +            
-        "result": result, +                "jsonrpc": "2.0", 
-        "id": request_id +                "result": result, 
-    }) +                "id": request_id, 
-except Exception as e: +            } 
-    return jsonify({ +        
-        "jsonrpc": "2.0", +    except Exception as e: 
-        "error": {"code": -32603, "message": str(e)}, +        return jsonify( 
-        "id": request_id +            
-    })+                "jsonrpc": "2.0", 
 +                "error": {"code": -32603, "message": str(e)}, 
 +                "id": request_id, 
 +            } 
 +        ) 
 + 
 + 
 +if __name__ == "__main__": 
 +    app.run(port=5000)
  
-if name == "main": 
-app.run(port=5000) 
 </sxh> </sxh>
  
Line 138: Line 149:
  
 <code bash> python server.py </code> <code bash> python server.py </code>
 +
 +===== JSON-RPC Client (Python) =====
 +
 +Create a file: //client.py//
 +
 +<sxh python>
 +import requests import json
 +
 +url = "http://localhost:5000/rpc"
 +
 +payload = {
 +  "jsonrpc": "2.0",
 +  "method": "add",
 +  "params": [10, 15],
 +  "id": 1
 +}
 +
 +response = requests.post(url, json=payload)
 +
 +print("Status:", response.status_code)
 +print("Response:", response.json())
 +</sxh>
 +
 +Expected output:
 +
 +<code> Status: 200 Response: {'jsonrpc': '2.0', 'result': 25, 'id': 1} </code>
 +
 +==== Error Codes in JSON-RPC 2.0 ====
 +
 +^ Code ^ Meaning ^
 +| -32700 | Parse error |
 +| -32600 | Invalid Request |
 +| -32601 | Method not found |
 +| -32602 | Invalid params |
 +| -32603 | Internal error |
tanszek/oktatas/iss_t/json-rpc.1772384789.txt.gz · Last modified: 2026/03/01 17:06 by knehez