Page Tools:
Wiki Relationships:
Admin Tools:
Article:Accessing JSON-RPC with Python
Contents |
What is JSON-RPC?
I've been examining the json-rpc project recently in hopes of using it in a project at work. JSON-RPC is similar to XML-RPC, except that it is using the JSON format for marshalling data rather than wrapping it in XML.
JSON (JavaScript Object Notation) is a lightweight format for passing around data. It allows for encoding of basic data types and includes strings, numbers, lists, and associative arrays. Another big benefit of JSON is that it is actually javascript code. So if you are doing AJAX applications you can easily and quickly access the data you want. (Rather than having to parse say XML. In fact most AJAX applications should probably be named AJAJ instead since they use JSON instead of XML for passing around data).
The current state of JSON-RPC
JSON-RPC is a work in progress, but appears to be off to a good start. I like the simplicity of the spec (it's about 2 pages long). Unfortunately the spec is so simple that implementations are already starting to add their own features, which make different implementations incompatible. (Using the Javascript client code from here to talk to a python server from http://json-rpc.org doesn't seem to work). Hopefully overtime the benefits of cross-language/implementation compatibility will show through (isn't that one of the benefits of a language neutral rpc mechanism such as this?) and the implementers will agree on a common feature set.
I'm a big proponent of WSGI and am writing server code in python using WSGI compliant servers, I preferred to have JSON-RPC also run in WSGI. So I'm in the process of creating an adapter to allow easy creation of JSON-RPC under WSGI. (I'll post the code for this soon).
Since I was new to JSON-RPC, I wanted to test my WSGI adapter and make sure it was still in accordance with the spec. Unfortunately, there was no python client code for JSON-RPC. So in order to test my server, I needed to write Javascript code. (Not fun and not necessarily easy to automate this testing). So after I got bored with that, I implemented a python client.
Creating a python library for client access
Fortunately xmlrpclib already exists. So instead of building client code from scratch, I basically took the xmlrpclib code and modified it to serialize its payload to json (using simplejson). Pretty simple. But it illustrates how powerful development can be when you combine opensource and python. (If I subclassed the code in xmlrpclib, instead of copying/pasting it, I could probably have the whole thing in less than 15 lines. But I currently feel that that would sacrifice understanding of what is going on in the code.)
Installing jsonrpclib
Download and unzip the tarball. Type:
easy_install setup.py
Using the client is pretty easy. Here's some sample code:
import jsonrpclib
s = jsonrpclib.ServerProxy("http://jsolait.net/services/test.jsonrpc")
reply = s.echo("foo bar")
print reply
This outputs:
{u'error': None, u'id': 5, u'result': u'foo bar'}
Future work
It literally took more time to write this article than write the client code. Thus I'm not making any claims to its robustness. I've included some test code that tests it against the json-rpc site and another site (using a java server). Accessing the java server actually fails, so figuring out what is happening might be a start. RPC mechanisms such as this are of little value if you can only use them with a certain language.
I'll do a writeup on the WSGI soon too, keep your eyes peeled.
About the author
Matt Harrison works for Spikesource as a software engineer. He's still waiting to do a YAML-RPC implementation.... Send any comments or suggestions to maharrison AT spikesource.com.
Most Recent |
Most Popular |
Most Active Categories |
| Back To Top | Add New Article | Printable Page |

Testing
