hjsonrpc module

Copyright (c) 2007 Jan-Klaas Kollhof

This file is part of jsonrpc.

jsonrpc is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this software; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

Heavily modified by Side Effects Software.

RPC clients use this module as follows:

s = hjsonrpc.ServiceProxy(“http://host/rpc”) result = s.method_name(arg1, arg2)
RPC servers use this method as follows:
class RPCService(object):

@hjsonrpc.rpcMethod def method_name(arg1, arg2):

pass

rpc_service = RPCService() server = hjsonrpc.createRPCServer(rpc_service, port, “/rpc”) server.serve_forever()

class hjsonrpc.HTTPRequestHandlerHelper(request, client_address, server)

Bases: BaseHTTPServer.BaseHTTPRequestHandler

This class contains useful helper methods that would otherwise be duplicated in subclasses.

readPostData()
sendNotFound()
sendResponse(code, content_type, response)
exception hjsonrpc.RPCException(error)

Bases: exceptions.Exception

This exception is raised to the RPC client when the called function raises an exception.

class hjsonrpc.RPCRequestHandler

Bases: hjsonrpc.HTTPRequestHandlerHelper

do_POST()
log_error(*args)
log_message(*args)
proxyToRPCService(url, service_name)

Act as a proxy to another RPC server, forwarding this request on.

Returns a “Service Unavailable” HTTP status code if the service isn’t running. The service name is only used for error messages.

sendServiceUnavailable(service_name)
class hjsonrpc.ServiceProxy(service_url, timeout=None, method_name=None)

Bases: object

This class acts as both the proxy to a “module” of functions and as a proxy to an individual function.

class hjsonrpc.WSGIApp(rpc_service)

Bases: object

A Web Server Gateway Interface that allows the RPC server to be plugged into a number of web servers and frameworks.

hjsonrpc.createRPCServer(rpc_service, port, rpc_path='/rpc', restrict_to_host='', request_handler_class=<class hjsonrpc.RPCRequestHandler>, logger=None, fail_on_exception=False)

Return a web server that will handle RPC requests and delegate them to rpc_service, an object whose methods are decorated with rpcMethod.

port: The port on which the web server will run. rpc_path: The web server path that will respond to RPC server requests. restrict_to_host: “localhost” if the server should only respond to

requests from the local machine, and “” if it should handle requests from any machine.
request_handler_class: Either RPCRequestHandler or a subclass. The server
will create instances of this class to handle RPC requests.
logger: The logging.Logger instance that will log messages, or None if
they should be printed to stderr. The messages are logged at the “info” level.
fail_on_exception: Whether or not the handle_request call should raise an
exception if one occurs while handling an RPC call. If not, the traceback will simply be logged an the server will continue to run.
hjsonrpc.rpcMethod(function)

Use this decorator on methods of the class providing the RPC functions.