Skip to content

wuan/txjsonrpc

 
 

Repository files navigation

txjsonrpc-ng

PyPI version Python versions License Quality gate Coverage Reliability rating OpenSSF Scorecard

Introduction

txjsonrpc-ng is an asynchronous JSON-RPC library for Python built on Twisted. It allows you to create async Python JSON-RPC servers and clients either over HTTP or directly on TCP with the Netstring protocol.

Features

  • 🚀 Asynchronous: Built on Twisted for high-performance async I/O
  • 🌐 Multiple Transports: HTTP and TCP (Netstring) support
  • 📋 Protocol Versions: Supports JSON-RPC pre-1.0, 1.0, and 2.0
  • 🔒 Authentication: Built-in authentication support
  • 🔍 Introspection: Built-in method introspection (listMethods, methodHelp, methodSignature)
  • 📚 jsonrpclib: Similar to Python's xmlrpclib for easy migration

Installation

Install from PyPI using pip:

pip install txjsonrpc-ng

Or using Poetry:

poetry add txjsonrpc-ng

Requirements:

  • Python 3.10 or higher
  • Twisted 24.11 or higher

Quick Start

Server Example (HTTP)

from twisted.web import server
from twisted.internet import reactor
from txjsonrpc_ng.web.jsonrpc import Handler

class ExampleHandler(Handler):
    def jsonrpc_echo(self, message):
        """Echo the message back"""
        return message

    def jsonrpc_add(self, a, b):
        """Add two numbers"""
        return a + b

if __name__ == '__main__':
    r = ExampleHandler()
    reactor.listenTCP(8080, server.Site(r))
    print("JSON-RPC server running on http://localhost:8080/")
    reactor.run()

Client Example (HTTP)

from twisted.internet import reactor
from txjsonrpc_ng.web.jsonrpc import Proxy

def printResult(result):
    print("Result:", result)
    reactor.stop()

def printError(error):
    print("Error:", error)
    reactor.stop()

if __name__ == '__main__':
    proxy = Proxy('http://localhost:8080/')
    d = proxy.callRemote('add', 5, 3)
    d.addCallback(printResult)
    d.addErrback(printError)
    reactor.run()

Examples

More examples are available in the examples/ directory:

  • examples/web/ - HTTP-based JSON-RPC
  • examples/tcp/ - TCP/Netstring-based JSON-RPC
  • examples/ssl/ - SSL-secured JSON-RPC
  • examples/webAuth/ - Authenticated JSON-RPC

Documentation

  • Installation: See docs/INSTALL.txt
  • Usage Guide: See docs/USAGE.txt
  • Specifications: See docs/specs/ for JSON-RPC protocol versions
  • Contributing: See CONTRIBUTING.md
  • Security: See SECURITY.md

License

txjsonrpc-ng is licensed under BSD and GPL. See LICENSE for details.

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

Support

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%