xmlrcp - how to marshall objects

Jean-Michel Pichavant jeanmichel at sequans.com
Sat Feb 6 19:48:22 EST 2010


Adam Tauno Williams wrote:
> On Fri, 2010-02-05 at 18:24 +0100, Jean-Michel Pichavant wrote:
>   
>> Jean-Michel Pichavant wrote:
>>     
>>> Deos anyone knows where to find an code sample describing how to 
>>> implement the interface to marshall one object into XMLRPC compliant 
>>> structures ?
>>> I googled without any success, and what google does not find does not 
>>> exist.
>>> Let say I have this very simple class:
>>> class Point:
>>>    def __init__(self, x, y):
>>>    self.x = x
>>>    self.y = y
>>> I've looked into xmlrpc code, I see  2 options:
>>> 1/ override the Marshaller class of client and server
>>> 2/ looks like the lib is supporting a WRAPPER list system, it uses to 
>>> Marshall Datetime & Binary object. Can it be possible to add its own 
>>> class (could require to emplement the 'encode' method)
>>> I sense I will spend much more time than required unless someone is 
>>> pointing me in the right direction.
>>>       
>> I realized I gave a poor example, actually the Point object is marshable 
>> (marshallable ? like to invent new words), xmlrpc will try to marshall 
>> using __dict__ if possible.
>> import os
>> class Point:
>>    def __init__(self, x, y):
>>    self.x = x
>>    self.y = y
>>    self.notMarshallable = os
>>     
>
> This example doesn't make any sense.  Why would you set a variable equal
> to an important module in a class named Point?
>
> What is it you are actually trying to accomplish?  If you are trying to
> create an object publishing environment then maybe something like -
>
> rpc = xmlrpclib.loads(payload, use_datetime=True)
> method = rpc[1].split('.')
> classname   = method[0]
> methodname = method[1]
> parameters  = rpc[0]
> classclass = eval(classname)
> handler = classclass()
> call = getattr(handler, method_name)
> result = apply(call, parameters)
> result = xmlrpclib.dumps(tuple([result]), methodresponse=True)
>
> Obviously add copious amounts of exception handling and a security
> model.
>   

I just took the first non marshallable object that came to my mind. So 
yes that makes no sense but it was an example.

I'll try to sitck to what I'm really trying to do.

I have 2 classes, defined on server side

class Pool:
    def foo():
       return 42

class Stream:
    def __init__(self, pool):
       """@param pool: the pool the stream belongs so."""
       self._pool = pool


I won't need any more detail I think for my example.

Now If I want to return a stream, sending it to the client, I can't 
because xmlrpclib won't be able to marshall _pool.
What I need is the sever being able to marshall a Stream.

I would like to overide the marshall method of that stream, so that 
instead of raising an error because self._pool is not marshallable, it 
sends an int instead identifying the pool on the server side.

class Pool:
    instances = {}

when marshalling a stream:
    id = some_unique_generated_int()
    Pool.instances[id] = self._pool
    self._pool = id # an int will be gracefully marshalled by the defaut 
marshaller
    # call here the default marshaller

Then, when unmarshalling, the server can reassign the _pool reference 
using the id provided by the client.

I hope I am being clear.

JM
   




More information about the Python-list mailing list