当前位置: 首页 > 工具软件 > simple-rpc > 使用案例 >

django jsonrpc ajax,JSON-RPC

葛修筠
2023-12-01

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.

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']}}

 类似资料: