You are here

Python XML-RPC Calls Via "requests"

requests is the latest and greatest Python module for performing HTTP requests. This new module cleans up a great deal of the mechanics required for network requests as well as streamling the often combersome syntax used by urlib2 and the httplib modules. Requests can easily be used for making XML-RPC requests, to the zOGI API for example. Using requests avoids the need for transport objects in order to work with proxy servers and more advanced authentication methods.

In order to make XML-RPC requests use the xmlrpclib module (included in the standard modules) as before but pass the actually network requests off to requests.

import requests, xmlrpclib

payload = xmlrpclib.dumps((10000, 0, ) , 'zogi.getObjectById', )

response = requests.request(
    'POST', 'http://coils.wmmi.net/RPC2',
    data = payload,
    headers = {'Content-Type': 'application/xml', },
    auth = ('username', 'password', ),
    timeout = 100,
    stream = False, 
)

if response.status_code == 200:
    result = xmlrpclib.loads(response.content, )[0][0]
    print result
else:
    # Some error occurred
    pass

Text 1: Example call to zogi.getObjectById method

As the example reveals there is no use of a server proxy object [as in traditional xmlrpclib usage], method calls are constructed manually using xmlrpclib.dumps but this is a small price to pay for the additional flexibility provided by requests.

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer