Yesterday I finished the Twisted JSON-RPC server and proxy for TCP. I
decided to go with the
Netstring protocol
for its simplicity and the security of declaring and limiting string
length.
Usage is very simple and almost identical to the HTTP-based Twisted
JSON-RPC usage. Here is some server code:
from twisted.application import service, internetfrom adytum.twisted import jsonrpc
class Example(jsonrpc.JSONRPC):
"""An example object to be published."""def jsonrpc_echo(self, x):
"""Return all passed args."""
return xfactory = jsonrpc.RPCFactory(Example)
application = service.Application("Example JSON-RPC Server")
jsonrpcServer = internet.TCPServer(7080, factory)
jsonrpcServer.setServiceParent(application)
And for the client:
from twisted.internet import reactor
from twisted.internet import deferfrom adytum.twisted.jsonrpc import Proxy
def printValue(value):
print "Result: %s" % str(value)def printError(error):
print 'error', errordef shutDown(data):
print "Shutting down reactor..."
reactor.stop()print "Making remote calls..."
proxy = Proxy('127.0.0.1', 7080)d = proxy.callRemote('echo', 'hey!')
d.addCallbacks(printValue, printError)
d.addCallback(shutDown)
reactor.run()
For a slightly more complex example (with subhandlers and
introspection) see:
the wiki.
Next on the list? For a truly secure solution, I am exploring the use
of the
Twisted
Perspective Broker for JSON-RPC.
No comments:
Post a Comment