Skip to main content

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"
)

func main() {
    // 클라이언트 생성
	var client struct {
		Hello func(ctx context.Context, params map[string]interface{}) (string, error)
		Add   func(ctx context.Context, params map[string]interface{}) (int, error)
	}
    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()

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.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

easy_jsonrpc-0.1.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file easy-jsonrpc-0.1.1.tar.gz.

File metadata

  • Download URL: easy-jsonrpc-0.1.1.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

Hashes for easy-jsonrpc-0.1.1.tar.gz
Algorithm Hash digest
SHA256 73807aa48b7e8c4f5cfd3fb59121aab0ee81627319cc9ba6308d62c1491104e5
MD5 f727e771bbbbbea2f29f71f4c04b19f8
BLAKE2b-256 54bb8f7a9b8e90deb3c7d7184769d75a8eeed359e8b775f9ad4dc6c2f4cbbb0d

See more details on using hashes here.

File details

Details for the file easy_jsonrpc-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: easy_jsonrpc-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.9

File hashes

Hashes for easy_jsonrpc-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dd9358804463ac97036128158547b2e19eb420d0a2c67378fe90625f0c2e9ee2
MD5 2390681added984e5f2afc265c47fc04
BLAKE2b-256 9157e8809c9f09fb86dcf25492065e710e5e503191ed0c48b362b5324cc5dbc3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page