Easy JSON-RPC library for Python and Go interoperability
Project description
Easy JSON-RPC
Python과 Go 클라이언트/서버 간의 호환성을 위한 간단한 JSON-RPC 라이브러리입니다.
특징
- Python 3.5+ 지원
- Python과 Go 간의 상호 운용성
- 간단한 API로 쉬운 사용
- 양방향 통신 및 알림(단방향) 지원
- 자동 메서드 등록 및 네임스페이스 관리
설치
pip install easy-jsonrpc
간단한 사용법
서버 예제
from easy_jsonrpc import EasyJSONRPCServer
# 간단한 함수 정의
def hello(params):
name = params.get("name", "World")
return "Hello, {}!".format(name)
def add(params):
a = params.get("a", 0)
b = params.get("b", 0)
return a + b
# 서버 초기화 및 실행
server = EasyJSONRPCServer('localhost', 8080, allow_go_client=True)
server.register_function(hello)
server.register_function(add)
server.start()
Python 클라이언트 예제
from easy_jsonrpc import EasyJSONRPCClient
# 클라이언트 초기화
client = EasyJSONRPCClient('http://localhost:8080')
# 메서드 호출
result = client.call('hello', {"name": "Alice"})
print(result) # 출력: Hello, Alice!
sum_result = client.call('add', {"a": 5, "b": 3})
print(sum_result) # 출력: 8
# 알림 전송 (응답 기다리지 않음)
client.notify('hello', {"name": "Notification"})
Go 클라이언트 예제
package main
import (
"context"
"fmt"
"github.com/filecoin-project/go-jsonrpc"
)
// API 인터페이스 정의
type API interface {
Hello(ctx context.Context, params map[string]interface{}) (string, error)
Add(ctx context.Context, params map[string]interface{}) (int, error)
}
func main() {
// 클라이언트 생성
var client API
closer, err := jsonrpc.NewClient(context.Background(), "http://localhost:8080", "", &client, nil)
if err != nil {
panic(err)
}
defer closer()
// 메서드 호출
params := map[string]interface{}{
"name": "Bob",
}
result, err := client.Hello(context.Background(), params)
if err != nil {
panic(err)
}
fmt.Println(result) // 출력: Hello, Bob!
// Add 메서드 호출
addParams := map[string]interface{}{
"a": 10,
"b": 20,
}
sum, err := client.Add(context.Background(), addParams)
if err != nil {
panic(err)
}
fmt.Println("Sum:", sum) // 출력: Sum: 30
}
클래스 등록 예제
from easy_jsonrpc import EasyJSONRPCServer
# 클래스 정의
class Calculator:
def add(self, params):
a = params.get("a", 0)
b = params.get("b", 0)
return a + b
def subtract(self, params):
a = params.get("a", 0)
b = params.get("b", 0)
return a - b
def multiply(self, params):
a = params.get("a", 0)
b = params.get("b", 0)
return a * b
# 서버 초기화 및 클래스 등록
server = EasyJSONRPCServer('localhost', 8080)
server.register_class(Calculator) # Calculator의 모든 메서드 등록
server.start()
라이센스
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
easy-jsonrpc-0.1.0.tar.gz
(5.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file easy-jsonrpc-0.1.0.tar.gz.
File metadata
- Download URL: easy-jsonrpc-0.1.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4995881de010c65c2b04fcb601ff55e4875bd77ebe6e5a2e8be0d44cad645dda
|
|
| MD5 |
13a548aa869ae01e6a33851c8148cdfe
|
|
| BLAKE2b-256 |
9641a35da221fb9128f5389cef6ed238ced35d86ec29f6d7b5ee61ecaf9ae459
|
File details
Details for the file easy_jsonrpc-0.1.0-py3-none-any.whl.
File metadata
- Download URL: easy_jsonrpc-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d38ad4d04681ca5fbac54f5205cb02658f6a43888f2379e612e831dd1a42fcc5
|
|
| MD5 |
1ebeb20b3e4cfe853ba6636a560f4fd0
|
|
| BLAKE2b-256 |
1632f09b553f71c410ad05f7e45f365311339fca0cda763db231d53351521e5d
|