MiscUtils.PickleRPC (version 1) | index /usr/local/share/webware/MiscUtils/PickleRPC.py

`PickleRPC.py
PickleRPC provides a Server object for connection to Pickle-RPC servers
for the purpose of making requests and receiving the responses.
>>> from MiscUtils.PickleRPC import Server
>>> server = Server('http://localhost:8080/Examples/PickleRPCExample')
>>> server.multiply(10,20)
200
>>> server.add(10,20)
30
See also: Server, Webkit.PickleRPCServlet, WebKit.Examples.PickleRPCExample
UNDER THE HOOD
Requests look like this:
{
'version': 1, # default
'action': 'call', # default
'methodName': 'NAME',
'args': (A, B, ...), # default = (,)
'keywords': {'A': A, 'B': B, ...} # default = {}
}
Only 'methodName' is required since that is the only key without a
default value.
Responses look like this:
{
'timeReceived': N,
'timeReponded': M,
'value': V,
'exception': E,
'requestError': E,
}
TimeReceived is the time the initial request was received.
TimeResponded is the time at which the response was finished, as
close to transmission as possible. The times are expressed as
number of seconds since the Epoch, e.g., time.time().
Value is whatever the method happened to return.
Exception may be 'occurred' to indicate that an exception
occurred, the specific exception, such as "KeyError: foo" or the
entire traceback (as a string), at the discretion of the server.
It will always be a non-empty string if it is present.
RequestError is an exception such as "Missing method
in request." (with no traceback) that indicates a problem with the
actual request received by the Pickle-RPC server.
Value, exception and requestError are all exclusive to each other.
SECURITY
Pickle RPC uses the SafeUnpickler class (in this module) to
prevent unpickling of unauthorized classes. By default, it
doesn't allow _any_ classes to be unpickled. You can override
allowedGlobals() or findGlobal() in a subclass as needed to
allow specific class instances to be unpickled.
Note that both Transport in this module and PickleRPCServlet in
WebKit are derived from SafeUnpickler.
CREDIT
The implementation of this module was taken directly from Python 2.2's
xmlrpclib and then transformed from XML-orientation to Pickle-orientation.
The zlib compression was adapted from code by Skip Montanaro that I found
here: http://manatee.mojam.com/~skip/python/`

Modules

``

zlib |  |  |

Classes

``

__builtin__.object
  SafeUnpickler
    Transport
      SafeTransport
  Server
  Server
exceptions.Exception(exceptions.BaseException)
  Error
    RequestError
    ResponseError
      InvalidContentTypeError
      ProtocolError(ResponseError, xmlrpclib.ProtocolError)

class Error(exceptions.Exception)
`` | `The abstract exception/error class for all PickleRPC errors.`

Method resolution order:
  Error
  exceptions.Exception
  exceptions.BaseException
  __builtin__.object

Data descriptors defined here:

__weakref__
  `list of weak references to the object (if defined)`

Methods inherited from exceptions.Exception:

__init__(...)
  `x.__init__(...) initializes x; see help(type(x)) for signature`

Data and other attributes inherited from exceptions.Exception:

__new__ = <built-in method __new__ of type object>
  `T.__new__(S, ...) -> a new object with type S, a subtype of T`

Methods inherited from exceptions.BaseException:

__delattr__(...)
  `x.__delattr__('name') <==> del x.name`

__getattribute__(...)
  `x.__getattribute__('name') <==> x.name`

__getitem__(...)
  `x.__getitem__(y) <==> x[y]`

__getslice__(...)
  `x.__getslice__(i, j) <==> x[i:j]
  Use of negative indices is not supported.`

__reduce__(...)

__repr__(...)
  `x.__repr__() <==> repr(x)`

__setattr__(...)
  `x.__setattr__('name', value) <==> x.name = value`

__setstate__(...)

__str__(...)
  `x.__str__() <==> str(x)`

__unicode__(...)

Data descriptors inherited from exceptions.BaseException:

__dict__

args

message

class InvalidContentTypeError(ResponseError)
`` | `Invalid content type error.`

Method resolution order:
  InvalidContentTypeError
  ResponseError
  Error
  exceptions.Exception
  exceptions.BaseException
  __builtin__.object

Methods defined here:

__init__(self, headers, content)

__repr__(self)

__str__ = __repr__(self)

Data descriptors inherited from Error:

__weakref__
  `list of weak references to the object (if defined)`

Data and other attributes inherited from exceptions.Exception:

__new__ = <built-in method __new__ of type object>
  `T.__new__(S, ...) -> a new object with type S, a subtype of T`

Methods inherited from exceptions.BaseException:

__delattr__(...)
  `x.__delattr__('name') <==> del x.name`

__getattribute__(...)
  `x.__getattribute__('name') <==> x.name`

__getitem__(...)
  `x.__getitem__(y) <==> x[y]`

__getslice__(...)
  `x.__getslice__(i, j) <==> x[i:j]
  Use of negative indices is not supported.`

__reduce__(...)

__setattr__(...)
  `x.__setattr__('name', value) <==> x.name = value`

__setstate__(...)

__unicode__(...)

Data descriptors inherited from exceptions.BaseException:

__dict__

args

message

class ProtocolError(ResponseError, xmlrpclib.ProtocolError)
`` | `RPC protocol error.`

Method resolution order:
  ProtocolError
  ResponseError
  Error
  xmlrpclib.ProtocolError
  xmlrpclib.Error
  exceptions.Exception
  exceptions.BaseException
  __builtin__.object

Data descriptors inherited from Error:

__weakref__
  `list of weak references to the object (if defined)`

Methods inherited from xmlrpclib.ProtocolError:

__init__(self, url, errcode, errmsg, headers)

__repr__(self)

Methods inherited from xmlrpclib.Error:

__str__(self)

Data and other attributes inherited from exceptions.Exception:

__new__ = <built-in method __new__ of type object>
  `T.__new__(S, ...) -> a new object with type S, a subtype of T`

Methods inherited from exceptions.BaseException:

__delattr__(...)
  `x.__delattr__('name') <==> del x.name`

__getattribute__(...)
  `x.__getattribute__('name') <==> x.name`

__getitem__(...)
  `x.__getitem__(y) <==> x[y]`

__getslice__(...)
  `x.__getslice__(i, j) <==> x[i:j]
  Use of negative indices is not supported.`

__reduce__(...)

__setattr__(...)
  `x.__setattr__('name', value) <==> x.name = value`

__setstate__(...)

__unicode__(...)

Data descriptors inherited from exceptions.BaseException:

__dict__

args

message

class RequestError(Error)
`` | `Errors originally raised by the server complaining about malformed requests.`

Method resolution order:
  RequestError
  Error
  exceptions.Exception
  exceptions.BaseException
  __builtin__.object

Data descriptors inherited from Error:

__weakref__
  `list of weak references to the object (if defined)`

Methods inherited from exceptions.Exception:

__init__(...)
  `x.__init__(...) initializes x; see help(type(x)) for signature`

Data and other attributes inherited from exceptions.Exception:

__new__ = <built-in method __new__ of type object>
  `T.__new__(S, ...) -> a new object with type S, a subtype of T`

Methods inherited from exceptions.BaseException:

__delattr__(...)
  `x.__delattr__('name') <==> del x.name`

__getattribute__(...)
  `x.__getattribute__('name') <==> x.name`

__getitem__(...)
  `x.__getitem__(y) <==> x[y]`

__getslice__(...)
  `x.__getslice__(i, j) <==> x[i:j]
  Use of negative indices is not supported.`

__reduce__(...)

__repr__(...)
  `x.__repr__() <==> repr(x)`

__setattr__(...)
  `x.__setattr__('name', value) <==> x.name = value`

__setstate__(...)

__str__(...)
  `x.__str__() <==> str(x)`

__unicode__(...)

Data descriptors inherited from exceptions.BaseException:

__dict__

args

message

class ResponseError(Error)
`` | `Unhandled exceptions raised when the server was computing a response. These will indicate errors such as: * exception in the actual target method on the server * malformed responses * non "200 OK" status code responses`

Method resolution order:
  ResponseError
  Error
  exceptions.Exception
  exceptions.BaseException
  __builtin__.object

Data descriptors inherited from Error:

__weakref__
  `list of weak references to the object (if defined)`

Methods inherited from exceptions.Exception:

__init__(...)
  `x.__init__(...) initializes x; see help(type(x)) for signature`

Data and other attributes inherited from exceptions.Exception:

__new__ = <built-in method __new__ of type object>
  `T.__new__(S, ...) -> a new object with type S, a subtype of T`

Methods inherited from exceptions.BaseException:

__delattr__(...)
  `x.__delattr__('name') <==> del x.name`

__getattribute__(...)
  `x.__getattribute__('name') <==> x.name`

__getitem__(...)
  `x.__getitem__(y) <==> x[y]`

__getslice__(...)
  `x.__getslice__(i, j) <==> x[i:j]
  Use of negative indices is not supported.`

__reduce__(...)

__repr__(...)
  `x.__repr__() <==> repr(x)`

__setattr__(...)
  `x.__setattr__('name', value) <==> x.name = value`

__setstate__(...)

__str__(...)
  `x.__str__() <==> str(x)`

__unicode__(...)

Data descriptors inherited from exceptions.BaseException:

__dict__

args

message

class SafeTransport(Transport)
`` | `Handle an HTTPS transaction to a Pickle-RPC server.`

Method resolution order:
  SafeTransport
  Transport
  SafeUnpickler
  __builtin__.object

Methods defined here:

make_connection(self, host, port=None, key_file=None, cert_file=None)
  `Create an HTTPS connection object from a host descriptor.`

Methods inherited from Transport:

parse_response(self, f)
  `Read response from input file and parse it.`

parse_response_gzip(self, f)
  `Read response from input file, decompress it, and parse it.`

request(self, host, handler, request_body, verbose=False, binary=False, compressed=False, acceptCompressedResponse=False)
  `Issue a Pickle-RPC request.`

send_content(self, connection, request_body, binary=False, compressed=False, acceptCompressedResponse=False)
  `Send content.`

send_host(self, connection, host)
  `Send host header.`

send_request(self, connection, handler, request_body)
  `Send request.`

send_user_agent(self, connection)
  `Send user-agent header.`

Data and other attributes inherited from Transport:

user_agent = 'PickleRPC/1 (by http://www.webwareforpython.org)'

Methods inherited from SafeUnpickler:

allowedGlobals(self)
  `Allowed class names.
  Must return a list of (moduleName, klassName) tuples for all
  classes that you want to allow to be unpickled.
  Example:
  return [('datetime', 'date')]
  allows datetime.date instances to be unpickled.`

findGlobal(self, module, klass)
  `Find class name.`

load(self, file)
  `Unpickle a file.`

loads(self, str)
  `Unpickle a string.`

Data descriptors inherited from SafeUnpickler:

__dict__
  `dictionary for instance variables (if defined)`

__weakref__
  `list of weak references to the object (if defined)`

class SafeUnpickler(__builtin__.object)

``

`Safe unpickler.
For security reasons, we don't want to allow just anyone to unpickle
anything. That can cause arbitrary code to be executed.
So this SafeUnpickler base class is used to control what can be unpickled.
By default it doesn't let you unpickle any class instances at all,
but you can create subclass that overrides allowedGlobals().
Note that the PickleRPCServlet class in WebKit is derived from this class
and uses its load() and loads() methods to do all unpickling.
`

Methods defined here:

allowedGlobals(self)
  `Allowed class names.
  Must return a list of (moduleName, klassName) tuples for all
  classes that you want to allow to be unpickled.
  Example:
  return [('datetime', 'date')]
  allows datetime.date instances to be unpickled.`

findGlobal(self, module, klass)
  `Find class name.`

load(self, file)
  `Unpickle a file.`

loads(self, str)
  `Unpickle a string.`

Data descriptors defined here:

__dict__
  `dictionary for instance variables (if defined)`

__weakref__
  `list of weak references to the object (if defined)`

class Server(__builtin__.object)

``

`uri [,options] -> a logical connection to an XML-RPC server
uri is the connection point on the server, given as
scheme://host/target.
The standard implementation always supports the "http" scheme.
If SSL socket support is available, it also supports "https".
If the target part and the slash preceding it are both omitted,
"/PickleRPC" is assumed.
See the module doc string for more information.
`

Methods defined here:

__getattr__(self, name)
  `Magic method dispatcher.
  Note: to call a remote object with an non-standard name,
  use result getattr(server, "strange-python-name")(args)`

__init__(self, uri, transport=None, verbose=False, binary=True, compressRequest=True, acceptCompressedResponse=True)
  `Establish a "logical" server connection.`

__repr__(self)

__str__ = __repr__(self)

Data descriptors defined here:

__dict__
  `dictionary for instance variables (if defined)`

__weakref__
  `list of weak references to the object (if defined)`

ServerProxy = class Server(__builtin__.object)

``

`uri [,options] -> a logical connection to an XML-RPC server
uri is the connection point on the server, given as
scheme://host/target.
The standard implementation always supports the "http" scheme.
If SSL socket support is available, it also supports "https".
If the target part and the slash preceding it are both omitted,
"/PickleRPC" is assumed.
See the module doc string for more information.
`

Methods defined here:

__getattr__(self, name)
  `Magic method dispatcher.
  Note: to call a remote object with an non-standard name,
  use result getattr(server, "strange-python-name")(args)`

__init__(self, uri, transport=None, verbose=False, binary=True, compressRequest=True, acceptCompressedResponse=True)
  `Establish a "logical" server connection.`

__repr__(self)

__str__ = __repr__(self)

Data descriptors defined here:

__dict__
  `dictionary for instance variables (if defined)`

__weakref__
  `list of weak references to the object (if defined)`

class Transport(SafeUnpickler)
`` | `Handle an HTTP transaction to a Pickle-RPC server.`

Method resolution order:
  Transport
  SafeUnpickler
  __builtin__.object

Methods defined here:

make_connection(self, host, port=None)
  `Create an HTTP connection object from a host descriptor.`

parse_response(self, f)
  `Read response from input file and parse it.`

parse_response_gzip(self, f)
  `Read response from input file, decompress it, and parse it.`

request(self, host, handler, request_body, verbose=False, binary=False, compressed=False, acceptCompressedResponse=False)
  `Issue a Pickle-RPC request.`

send_content(self, connection, request_body, binary=False, compressed=False, acceptCompressedResponse=False)
  `Send content.`

send_host(self, connection, host)
  `Send host header.`

send_request(self, connection, handler, request_body)
  `Send request.`

send_user_agent(self, connection)
  `Send user-agent header.`

Data and other attributes defined here:

user_agent = 'PickleRPC/1 (by http://www.webwareforpython.org)'

Methods inherited from SafeUnpickler:

allowedGlobals(self)
  `Allowed class names.
  Must return a list of (moduleName, klassName) tuples for all
  classes that you want to allow to be unpickled.
  Example:
  return [('datetime', 'date')]
  allows datetime.date instances to be unpickled.`

findGlobal(self, module, klass)
  `Find class name.`

load(self, file)
  `Unpickle a file.`

loads(self, str)
  `Unpickle a string.`

Data descriptors inherited from SafeUnpickler:

__dict__
  `dictionary for instance variables (if defined)`

__weakref__
  `list of weak references to the object (if defined)`

Functions

``

StringIO(...)
  `StringIO([s]) -- Return a StringIO-like stream for reading or writing`

Unpickler(...)
  `Unpickler(file) -- Create an unpickler.`

dumps(...)
  `dumps(obj, protocol=0) -- Return a string containing an object in pickle format.
  See the Pickler docstring for the meaning of optional argument proto.`

Data
`` |  | __version__ = 1