Search code examples
pythonapixml-rpc

Passing parameters using XML-RPC client in Python


I have an API key, which I can use via Chrome (XML RPC Client extension, with JSON array input) or with Firefox extension (RESTClient extension, with XML data input). I want to do it in Python.

I can list methods, but I have no idea how to pass complex things.

Here is the code which returns methods:

import xmlrpc.client

with xmlrpc.client.ServerProxy("http://a4a.test.clickonometrics.pl/api/xmlrpc.php") as proxy:
    response=proxy.system.listMethods()
    print(response)

I want to use method "publisher.getStats" and pass JSON Array:

["bOpd4YbxbQXZxa7n1Aj4PbsRbviz1Jlk",{"perspective":"campaigns","date_start":"2016-08-01","date_end":"2016-12-31","ids":["534"],"group":"placements"}]

It works 1:1 as I described in Chrome XML-RPC Client extension.

How to do it in Python?


Solution

  • I finally managed to do it.

    The method name should be passed like proxy.methodname, and the parameters in regular brackets, just without []. Really simple, but it took me some time.

    Working code:

    import xmlrpc.client
    
    with xmlrpc.client.ServerProxy("http://a4a.test.clickonometrics.pl/api/xmlrpc.php") as proxy:
        response=proxy.publisher.getStats("bOpd4YbxbQXZxa7n1Aj4PbsRbviz1Jlk",{"perspective":"campaigns","date_start":"2016-08-01","date_end":"2016-12-31","ids":["534"],"group":"placements"})
        print(response)