TwistedWeb2
JSON-RPC for twisted.web2
Dependencies
- Twisted (+twisted.web2)
- simplejson
Notes
The original tests for twisted.web2.xmlrpc were also included/adapted and pass successfully.
Import
>>> from txjsonrpc.web2 import jsonrpc
Usage
Server
This is a direct copy from twisted.web2.xmlrpc and you use it identically, substituting JSONRPC and jsonrpc everywhere you see XMLRPC and xmlrpc. The docs for t.w2.xmlrpc are not up yet, but here are some example usages:
from twisted.web2 import server from twisted.web2.channel import http from twisted.application import service, internet from txjsonrpc.web2 import jsonrpc class Example(jsonrpc.JSONRPC): """An example object to be published.""" addSlash = True def jsonrpc_echo(self, request, x): """Return all passed args.""" return x def jsonrpc_add(self, request, a, b): """Return sum of arguments.""" return a + b site = server.Site(Example()) chan = http.HTTPFactory(site) application = service.Application("Example JSON-RPC Server") jsonrpcServer = internet.TCPServer(7080, chan) jsonrpcServer.setServiceParent(application)
Save this as server.tac and run with the following command:
$ twistd -noy server.tac
Client
As far as I know, twisted.web2 doesn't have a (functional? good? any?) client yet. For this reason, t.w2.xmlrpc and a.t.w2.jsonrpc unit tests both use t.w for the client. We will do the same here:
from twisted.internet import reactor from txjsonrpc.web.jsonrpc import Proxy def printValue(value): print repr(value) reactor.stop() def printError(error): print 'error', error reactor.stop() proxy = Proxy('http://127.0.0.1:7080/') proxy.callRemote('add', 3, 5).addCallbacks(printValue, printError) reactor.run()
Save this file as client.py and run it from the command line. You should see an '8' printed to stdout.
txJSON-RPC/TwistedWeb2 (last edited 2008-08-06 16:31:57 by localhost)