Code

Simply see XML-RPC . Use the SimpleJSONRPCServer instead of SimpleXMLRPCServer and you are done!

You'll also want JSON RPC Client too.

$ cat testjsonrpc.py 
import sys
import jsonrpclib
rpc_srv = jsonrpclib.ServerProxy("http://localhost:8000/json_rpc_srv/")
result = rpc_srv.multiply( int(sys.argv[1]), int(sys.argv[2]))
print "%d * %d = %d" % (int(sys.argv[1]), int(sys.argv[2]), result['result'])

There you go!

django-json-rpc

Another, easier to use JSON-RPC implementation.

django-json-rpc @ github

Features:

  • Simple, pythonic API
  • Support for Django authentication
  • Mostly supports JSON-RPC 1.1 spec
  • Proxy to test your JSON Service

The basic API:

  ### myproj/myapp/views.py
  
  from jsonrpc import jsonrpc_method
  
  @jsonrpc_method('myapp.sayHello')
  def whats_the_time(request, name='Lester'):
    return "Hello %s" % name
  
  @jsonrpc_method('myapp.gimmeThat', authenticated=True)
  def something_special(request, secret_data):
    return {'sauce': ['authenticated', 'sauce']}
  
  
  ### myproj/urls.py
  
  from jsonrpc import jsonrpc_site
  import myproj.myapp.views # you must import the views that need connected
  
  urls += patterns('', (r'^json/', jsonrpc_site.dispatch))

To test your service:

  >>> from jsonrpc.proxy import ServiceProxy
  
  >>> s = ServiceProxy('http://localhost:8080/json/')
  
  >>> s.myapp.sayHello('Sam')
  {u'error': None, u'id': u'jsonrpc', u'result': u'Hello Sam'}
  
  >>> s.myapp.gimmeThat('username', 'password', 'test data')
  {u'error': None, u'id': u'jsonrpc', u'result': {u'sauce': [u'authenticated', u'sauce']}}
Last modified 5 years ago Last modified on 07/06/09 14:53:06