About

Welcome to Panela, Matt Harrison's take on mostly Open Source, Linux, Python, innovation in those areas, other buzzwords and Dick Proenneke. It comes complete with the illustrations as needed. Note the opinions expressed here are merely my opinions and not the opinions of my employer.

about Matt

Calendar

««Aug 2008»»
SMTWTFS
      12
3456789
10111213141516
17181920212223
24
25
2627282930
31

My Top Tags

                                       

Mailing List

My RSS Feeds








JSON-RPC python client code

posted 2006.03.27 Mon
Like Duncan, I've also been messing around with JSON-RPC lately. I'm got most of a WSGI implementation done and a python client library done. I have an article describing the client code here. For pythoneers, I've got a question: This code is largely based on xmlrpclib. It could easily be implemented as a subclass of xmlrpclib.ServerProxy (instead of just copying the code). So my question is should I subclass it? I'm not doing it right now, since I think it might actually diverge from the xmlrpc code at some point, and it also is a little easier to understand since all the code is in one single file (in my opinion). Opinions? Ideas? Thoughts?

tags:        

links: digg this    del.icio.us    reddit




1. Peter Fein left...
2006.03.27 Mon 7:10 pm

Subclass, or use it as an internal implementation detail.

But definitely don't copy & paste the code. ;)


2. Duncan McGreggor left...
2006.03.27 Mon 9:32 pm :: http://oubiwann.blogspot.com/

Hey man, I read your linked article, and have a comment about this:

"Unfortunately, there was no python client code for JSON-RPC."

There actually is, at the json-rpc.org site. I really hate the usage though:

from jsonrpc.proxy import ServiceProxy
import test_api

server = ServiceProxy('jsonrpc://127.0.0.1:9002', test_api.Service)
print server.echo('a dogcow says "Moof!"')
print server.add(1, 7)
server.disconnect()

But that was only one of the reasons I decided to write my own ;-)

Like you, I did the same thing: copied the one or two parts of xmlrpclib that I needed. It's not a long-term plan, though, and I did it while prototyping because I wasn't sure how I might need to change things. I had decided to change xmlrpclib.Fault, but then changed my mind and reverted, so there's no reason not to do an import (guess I'll go change that now...). FWIW, I'm also using a few of xmlrpclib's error codes.

Here's what they do in twisted.web.xmlrpc:

# Useful so people don't need to import xmlrpclib directly
Fault = xmlrpclib.Fault
Binary = xmlrpclib.Binary
Boolean = xmlrpclib.Boolean
DateTime = xmlrpclib.DateTime

And, finally, one last bit of code ;-) This is what I did for marshalling/unmarshalling, and I'd be interested to see how you did things differently:

def dumps(obj, **kws):
    if isinstance(obj, Exception):
        obj = {'fault': obj.__class__.__name__,
            'faultCode': obj.faultCode,
            'faultString': obj.faultString}
    return simplejson.dumps(obj, **kws)

def loads(s, **kws):
    unmarshalled = simplejson.loads(s, **kws)
    if unmarshalled.has_key('fault'):
        raise Fault(unmarshalled['faultCode'],
            unmarshalled['faultString'])
    return unmarshalled


3. Duncan McGreggor left...
2006.03.27 Mon 10:17 pm :: http://oubiwann.blogspot.com/

Hey Matt,

Bob Ippolito commented on my twisted json-rpc blog post. He's added "object_hook" to JSONDecoder; from the docstring:

        ``object_hook``, if specified, will be called 
        with the result of every JSON object decoded and 
        its return value will be used in place of the 
        given ``dict``.  This can be used to provide custom
        deserializations (e.g. to support JSON-RPC class 
        hinting).

This is very cool and I'm going to write stuff to use this. Thought you'd appreciate a heads-up ;-)


4. Some Dude left...
2006.09.29 Fri 2:05 pm

Anyone know what's up with the javascript implementations of json-rpc clients? are they operating off a different spec? I tried getting one to talk to a python server and it seems to want something called system.listmethods -- very confusing.