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:08] – [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 81: Line 78:
 from flask import Flask, request, jsonify 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": 
 +        return jsonify( 
 +            { 
 +                "jsonrpc": "2.0", 
 +                "error": {"code": -32600, "message": "Invalid Request"}, 
 +                "id": data.get("id"), 
 +            } 
 +        ) 
 + 
 +    method_name = data.get("method"
 +    params = data.get("params", []) 
 +    request_id = data.get("id")
  
-if data.get("jsonrpc") != "2.0"+    if method_name not in methods
-    return jsonify({ +        return jsonify( 
-        "jsonrpc": "2.0", +            
-        "error": {"code": -32600, "message": "Invalid Request"}, +                "jsonrpc": "2.0", 
-        "id": data.get("id") +                "error": {"code": -32601, "message": "Method not found"}, 
-    })+                "id": request_id, 
 +            } 
 +        )
  
-method_name data.get("method"+    try: 
-params = data.get("params", []) +        result methods[method_name](*params
-request_id = data.get("id")+        return jsonify( 
 +            { 
 +                "jsonrpc": "2.0", 
 +                "result": result, 
 +                "id": request_id
 +            } 
 +        ) 
 +    except Exception as e: 
 +        return jsonify( 
 +            { 
 +                "jsonrpc": "2.0", 
 +                "error": {"code": -32603, "message": str(e)}, 
 +                "id": request_id, 
 +            } 
 +        )
  
-if method_name not in methods: 
-    return jsonify({ 
-        "jsonrpc": "2.0", 
-        "error": {"code": -32601, "message": "Method not found"}, 
-        "id": request_id 
-    }) 
  
-try: +if __name__ == "__main__": 
-    result methods[method_name](*params) +    app.run(port=5000)
-    return jsonify({ +
-        "jsonrpc": "2.0", +
-        "result": result, +
-        "id": request_id +
-    }) +
-except Exception as e: +
-    return jsonify({ +
-        "jsonrpc": "2.0", +
-        "error": {"code": -32603, "message": str(e)}, +
-        "id": request_id +
-    })+
  
-if name == "main": 
-app.run(port=5000) 
 </sxh> </sxh>
  
Line 147: Line 157:
 import requests import json import requests import json
  
-url = "http://localhost:5000/rpc +url = "http://localhost:5000/rpc"
-"+
  
 payload = { payload = {
-"jsonrpc": "2.0", +  "jsonrpc": "2.0", 
-"method": "add", +  "method": "add", 
-"params": [10, 15], +  "params": [10, 15], 
-"id": 1+  "id": 1
 } }
  
tanszek/oktatas/iss_t/json-rpc.1772384915.txt.gz · Last modified: 2026/03/01 17:08 by knehez