谁能推荐一些简单的代码来使用扭曲设置简单的JSON RPC客户端和服务器?

我找到了TXJSON-RPC,但是我想知道有人使用其中一些ANC是否有一些经验可以推荐一些东西。

有帮助吗?

解决方案

TXJSONRPC很棒。我使用它,它有效。我建议您尝试一下。

服务器:

from txjsonrpc.web import jsonrpc
from twisted.web import server
from twisted.internet import reactor

class Math(jsonrpc.JSONRPC):
    """
    An example object to be published.
    """
    def jsonrpc_add(self, a, b):
        """
        Return sum of arguments.
        """
        return a + b

reactor.listenTCP(7080, server.Site(Math()))
reactor.run()

客户:

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

def printValue(value):
    print "Result: %s" % str(value)

def printError(error):
    print 'error', error

def shutDown(data):
    print "Shutting down reactor..."
    reactor.stop()

proxy = Proxy('http://127.0.0.1:7080/')

d = proxy.callRemote('add', 3, 5)
d.addCallback(printValue).addErrback(printError).addBoth(shutDown)
reactor.run()

作为奖励,我将留下一些选择:放大器。http://amp-protocol.net

其他提示

如果您正在寻找独立于框架的方法, 这个自由 我推(使用Mixin)可能有帮助:

气旋, , 一个 龙卷风 Async Web服务器实现使用Twisted编写,具有使用Python JSON/SimpleJson模块的内置JSON-RPC请求处理程序。示例服务器和客户端代码是 这里.

Wikipedia在Python上列出了许多实施: https://en.wikipedia.org/wiki/json-rpc#implementations

就是说, txjason 感觉就像是与扭曲的最佳融合在一起的感觉。例如,它似乎支持订单响应开箱即用。其中大部分将使用六个便携式python3。最可怕的部分是参数验证,无论如何,它并未在普通公共API中暴露。

对我来说,这比“库”更好,谈到客户。

    TESTDATA = {'id': 1234,
                'method': 'getbalance',
                }
    URL = 'http://localhost:7777'

    d= getPage(URL,method="POST",postdata=json.dumps(TESTDATA))
    d.addBoth(lambda x :print(json.loads(x)))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top