2006
01.19
01.19
I had been looking for a way to call a function using XML-RPC in Python with the xmlrpclib class. The problem was I wanted to call it from within an organization behind an HTTP proxy, and the python object (ServerProxy) didn’t allow it by default.
Nevertheless, if we look closer to the class constructor:
class ServerProxy( uri[, transport[, encoding[, verbose[, allow_none]]]])
we realize it’s possible to define an optional Transport object. And that was the solution, as I found explined in this (Japanese) page: XML-RPC over the proxy server. I don’t speak a word of Japanese, simply I undesrtood the code. ![]()
It defines a derivated transport class:
import xmlrpclib class ProxiedTransport(xmlrpclib.Transport): # Put here an identification string for your application user_agent = 'hogehoge' def set_proxy(self, proxy): self.proxy = proxy def make_connection(self, host): self.realhost = host import httplib return httplib.HTTP(self.proxy) 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) def __init__(self, proxy): self.proxy = proxy
And now it’s possible to do XML-RPC over a proxy:
# Creates the XML-RPC server connection server = xmlrpclib.ServerProxy('http://xmlrpc-c.sourceforge.net/api/sample.php', transport = ProxiedTransport('proxy.server:3128') # Lists remote server exported methods for method in server.system.listMethods(): print method, server.system.methodHelp(method)
Related posts: