XML-RPC API - Python Example

Python Example
S
Written by Sergiiy
Updated 11 months ago

  * Digest Authentication for Trusted Numbers Manipulation and CLI Mapping

See introduction to XMLAPI for details on Authentication:

#!/usr/bin/env python

# this script requires httplib2 to be installed
# username should be Web Login of the Customer, password - API password
# 1.2.3.4 should be replaced with target IP/hostname of Flysip
import httplib2
try:
    from urllib.parse import splituser, splitpasswd
    from xmlrpc.client import ServerProxy, getparser, ProtocolError
except ImportError:
    from urllib import splituser, splitpasswd
    from xmlrpclib import ServerProxy, getparser, ProtocolError

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

        h = httplib2.Http(disable_ssl_certificate_validation=True)

        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:[email protected]/xmlapi/xmlapi", transport)

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

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

Python

* Trusted Authentication for Trusted Numbers Manipulation and CLI Mapping

In this example i_customer should be supplied to selected XMLAPI method, which is mandatory for most methods in trusted mode. 

#!/usr/bin/env python
# this script requires httplib2 to be installed
# 1.2.3.4 should be replaced with target IP/hostname of Flysip
import httplib2
try:
    from xmlrpc.client import ServerProxy, getparser, ProtocolError
except ImportError:
    from xmlrpclib import ServerProxy, getparser, ProtocolError

class HTTPSTrustedAuthTransport:
    def request(self, host, handler, request_body, verbose=0):
        h = httplib2.Http(disable_ssl_certificate_validation=False)
        if verbose:
            h.debuglevel = 1
        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 = HTTPSTrustedAuthTransport()
client = ServerProxy("https://1.2.3.4/xmlapi/xmlapi", transport)

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

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

Python

* Manage Call-Back Calls

#!/usr/bin/env python

# this script requires httplib2 to be installed from http://code.google.com/p/httplib2/
import httplib2
try:
    from urllib.parse import splituser, splitpasswd
    from xmlrpc.client import ServerProxy, getparser, ProtocolError
except ImportError:
    from urllib import splituser, splitpasswd
    from xmlrpclib import ServerProxy, getparser, ProtocolError

my_number = '12061234567'
destination_number = '12067654321'
authname = '000124'

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

        h = httplib2.Http(disable_ssl_certificate_validation=True)

        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@flysip_ip/xmlapi/xmlapi", transport)
res = client.make2WayCallback({ 'authname' : authname,
                                'cld_first' : my_number,
                                'cld_second' : destination_number,
                                'credit_time' : 3600 # call should not continue more than an hour
                               })
if res['result'] == 'OK':
    print (res['i_callback_request'])

Python

* Refunding an Account

#!/usr/bin/env python

# this script requires httplib2 to be installed from http://code.google.com/p/httplib2/
import httplib2
try:
    from urllib.parse import splituser, splitpasswd
    from xmlrpc.client import ServerProxy, getparser, ProtocolError
except ImportError:
    from urllib import splituser, splitpasswd
    from xmlrpclib import ServerProxy, getparser, ProtocolError

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

        h = httplib2.Http(disable_ssl_certificate_validation=True)

        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@flysip_ip/xmlapi/xmlapi", transport)
res = client.accountAddFunds({ 'i_account' : 34, 'amount' : 1.23, 'currency' : 'USD', 'payment_notes' : 'Test credit' })
print (res['result'])
Did this answer your question?