Using Python xmlrpclib to build XMLRPC clients for a server with variable RPC names

Chris Rebert clp2 at rebertia.com
Sun Oct 9 14:00:01 EDT 2011


On Sun, Oct 9, 2011 at 6:30 AM, John P. Crackett
<jpc at parallelthoughts.org> wrote:
> I need to write prototype XMLRPC clients using xmlrpclib for a server
> that has variable RPC names and I'd like to use Python as the
> prototyping tool.  I've searched but can't find any relevant advice
> online.  Any pointers would be gratefully received; details follow.
>
> The server in question constructs method names dynamically using the
> names of objects created by the client.
<snip>
> As an added complication, object names need to be concatenated using "-"
> as a separator to form compound RPC names.  Following on from the
> example above, this could lead to:
>
> api = xmlrpclib.ServerProxy(serverURL)
>
> api. createObjects("foo", "far")
>
> api.foo-bar.callOne()
>
> Where I don't know, again, what "foo" and "bar" will actually be in
> advance.  "foo-bar" is obviously not a valid Python method name and this
> is preventing me from hard-coding just to get a prototype going.

Try using getattr():
    getattr(api, "foo-bar").callOne()

For reference, getattr() satisfies the following identity:
    x.y === getattr(x, "y")
except that getattr() does not itself enforce/require that its second
argument be a valid Python identifier.

Cheers,
Chris
--
http://rebertia.com



More information about the Python-list mailing list