Loading ...
Sorry, an error occurred while loading the content.
 

json-rpc dispatcher and client in python

Expand Messages
  • Mikeal Rogers
    I wrote some generally useful code recently which i figured I should share. http://windmill.osafoundation.org/trac/browser/windmill/trunk/
    Message 1 of 3 , Jan 2, 2007
      I wrote some generally useful code recently which i figured I should
      share.

      http://windmill.osafoundation.org/trac/browser/windmill/trunk/
      windmill/server/jsonrpc.py is a json-rpc dispatcher that works mostly
      like the SimpleXMLRPCDispatcher in SimpleXMLRPCServer in stdlib.

      You'll also find a WSGI application callable class that uses the
      dispatcher to fullfill HTTP json-rpc requests.

      system.describe is fully implemented.

      http://windmill.osafoundation.org/trac/browser/windmill/trunk/
      windmill/tools/json_tools.py contains a ServerProxy class that
      implements a json-rpc client that works _exactly_ like the xmlrpc
      client in xmlrpclib in stdlib.

      Both require simplejson.

      All code was written by me at OSAF (www.osafoundation.org) and is
      Apache License 2.0.

      -Mikeal
    • Bob Gailer
      ... I appreciate your sharing this code. Unfortunately I have no idea what to do with it or how to install it. Could you provide a little guidance? Is it
      Message 2 of 3 , Jan 3, 2007
        Mikeal Rogers wrote:
        > I wrote some generally useful code recently which i figured I should
        > share.
        >
        I appreciate your sharing this code. Unfortunately I have no idea what
        to do with it or how to install it. Could you provide a little guidance?
        Is it intended to run under Apache? Can it be run under mod_python? (or
        some other way that makes it a long-running program?)
        > http://windmill.osafoundation.org/trac/browser/windmill/trunk/
        > windmill/server/jsonrpc.py is a json-rpc dispatcher that works mostly
        > like the SimpleXMLRPCDispatcher in SimpleXMLRPCServer in stdlib.
        >
        > You'll also find a WSGI application callable class that uses the
        > dispatcher to fullfill HTTP json-rpc requests.
        >
        > system.describe is fully implemented.
        >
        > http://windmill.osafoundation.org/trac/browser/windmill/trunk/
        > windmill/tools/json_tools.py contains a ServerProxy class that
        > implements a json-rpc client that works _exactly_ like the xmlrpc
        > client in xmlrpclib in stdlib.
        >
        > Both require simplejson.
        >
        > All code was written by me at OSAF (www.osafoundation.org) and is
        > Apache License 2.0.
        >
        > -Mikeal
        >
        >
        >
        > Yahoo! Groups Links
        >
        >
        >
        >
        >


        --
        Bob Gailer
        510-978-4454
      • Mikeal Rogers
        ... The dispatcher is the underlying object for running any python method over json-rpc. You send it methods to be added for dispatch, or you can send it a
        Message 3 of 3 , Jan 3, 2007
          I appreciate your sharing this code. Unfortunately I have no idea what
          to do with it or how to install it. Could you provide a little guidance?
          Is it intended to run under Apache? Can it be run under mod_python? (or
          some other way that makes it a long-running program?


          The dispatcher is the underlying object for running any python method over json-rpc. You send it methods to be added for dispatch, or you can send it a class instance and every method in the class will be added for dispatch.

          def my_function():
          print 'my_function called'
          return {'blah':'I ran the function'}

          class MyClass(object):
          def first_method(self, arg):
          print 'first_method called %s' % arg
          return {'blah_result':arg}
          def second_method(self, test='blah'):
          print 'second_method called %s' % test
          return [test]

          >>> dispatcher = JSONRPCDispatcher()
          >>> dispatcher.register_method(my_function)
          >>> myclass_instance = MyClass()
          >>> dispatcher.register_instance(myclass_instance)

          Now if a json-rpc request body is sent to the dispatcher it will run any of these methods.

          >>> body = ' {"method":"second_method", "params":{"test":"new_test"}, "id":1} '
          >>> result = dispatcher.dispatch(body)
          second method called new_test
          >>> result
          ' {"result":["new_test"], "id":1}'

          Basically what it does is register methods, then converts json-rpc requests to proper method arguments and dispatch them to the proper method. If that method returns an object the object is serialized to json. If 'id' is specified the dispatcher handles carrying the id to the response. If an error occurs a proper jsonrpc error is returned.

          I didn't have time to show examples but there are some other niceties. It supports both positional and keyword arguments, and if the last argument in a list of arguments is a dict and the method requires more arguments than given it'll try to expand the dict to fill out the keyword arguments.

          As you can see this isn't ready to just be dropped into apache or mod_python directly. I do have a WSGI callable class that will serve out the methods in the dispatcher over HTTP, if you're unfamiliar with WSGI i suggest you check out http://wsgi.org/wsgi and http://www.python.org/dev/peps/pep-0333/.

          WSGI is a common server interface for python, similiar to CGI but more feature rich. Python stdlib comes with a reference implementation called wsgiref, here is another list of WSGI servers http://wsgi.org/wsgi/Servers -- including one for mod_python.

          If you're wondering about how to use the json-rpc client i wrote check out http://docs.python.org/lib/module-xmlrpclib.html . The json-rpc Proxy class should work just like the xmlrpc one in xmlrpclib.

          -Mikeal










        Your message has been successfully submitted and would be delivered to recipients shortly.