任何人都可以推荐一些简单的代码来设置一个简单的JSON RPC客户端和服务器使用扭曲?Python Twisted JSON RPC
我发现txJSON-RPC,但我想知道如果有人有一些使用这些anc的经验可以推荐的东西。
任何人都可以推荐一些简单的代码来设置一个简单的JSON RPC客户端和服务器使用扭曲?Python Twisted 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
如果你正在寻找一个框架,独立的做法,this lib我推(使用的mixin)可能会有所帮助:
维基百科有一堆的蟒上市实现: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)))
工作很好,除了两个小的变化。我需要改变 类Math(jsonrpc.jsonRPC): 到 类Math(jsonrpc.JSONRPC): 服务器 和 端口在客户端和服务器端的代码不匹配,让他们在一个同意和它的工作原理很好 – 1337Rooster 2011-01-19 22:31:42