Python Example

Last updated by FlySIP Support on October 31, 2019 08:41

Python example

# this script requires httplib2 to be installed from http://code.google.com/p/httplib2/
import httplib2
import urllib
from xmlrpclib import ServerProxy, getparser, ProtocolError

class HTTPSDigestAuthTransport:
    def request(self, host, handler, request_body, verbose=0):
        auth, host = urllib.splituser(host)
        username, password = urllib.splitpasswd(auth)

        h = httplib2.Http()
        if verbose:
            h.debuglevel = 1
        h.add_credentials(username, password)

        resp, content = h.request("https://" + host + handler, "POST", body=request_body,
                                  headers={'content-type':'text/xml'})

        if resp.status != 200:
            raise ProtocolError("https://" + host + handler,
                                resp.status, resp.reason, None)

        p, u = getparser(0)
        p.feed(content)

        return u.close()

transport = HTTPSDigestAuthTransport()
client = ServerProxy("https://username:password@1.2.3.4/xmlapi/xmlapi", transport)

# add a mapping
res = client.addCLIMapping({ 'i_account' : 1234, 'cli' : '123456', 'lang' : 'en' })
print res['result']

# retrieve list of trusted numbers
res = client.listCLIMappings({ 'i_account' : 1234 })
if res['result'] == 'OK':
    for cli, lang in res['list']:
        print cli, lang