18.24.9 Example of Client Usage

##
18.24.9 Example of Client Usage

    # simple test program (from the XML-RPC specification)
    from xmlrpclib import ServerProxy, Error

    # server = ServerProxy("http://localhost:8000") # local server
    server = ServerProxy("http://betty.userland.com")

    print server

    try:
        print server.examples.getStateName(41)
    except Error, v:
        print "ERROR", v

To access an XML-RPC server through a proxy, you need to define a custom transport. The following example, written by NoboNobo, shows how:

    import xmlrpclib, httplib

    class ProxiedTransport(xmlrpclib.Transport):
        def set_proxy(self, proxy):
            self.proxy = proxy
        def make_connection(self, host):
            self.realhost = host
    	h = httplib.HTTP(self.proxy)
    	return h
        def send_request(self, connection, handler, request_body):
            connection.putrequest("POST", 'http://%s%s' % (self.realhost, handler))
        def send_host(self, connection, host):
            connection.putheader('Host', self.realhost)

    p = ProxiedTransport()
    p.set_proxy('proxy-server:8080')
    server = xmlrpclib.Server('http://time.xmlrpc.com/RPC2', transport=p)
    print server.currentTime.getCurrentTime()