[Twisted-Python] access an XMLRPC service through an HTTP proxy

Antoine Pitrou solipsis at pitrou.net
Wed Dec 29 07:49:17 EST 2004

> I'm using twisted.web.xmlrpc.Proxy to connect to an XMLRPC service.
> Unfortunately there are situations in which I have to go through an HTTP
> proxy, yet there doesn't seem to be an option for this (and looking at
> the code, the connection to the remote server is quite "harwired").

Replying to myself... The following seems to work (I didn't test with

import urlparse
import twisted.web.xmlrpc as xmlrpc

class ProxiedXMLRPC:
    A Proxy for making remote XML-RPC calls accross an HTTP proxy.

    Pass the URL of the remote XML-RPC server to the constructor,
    as well as the proxy host and port.

    Use proxy.callRemote('foobar', *args) to call remote method
    'foobar' with *args.

    def __init__(self, reactor, url, proxy_host, proxy_port):
        self.reactor = reactor
        self.url = url
        self.proxy_host = proxy_host
        self.proxy_port = proxy_port
        parts = urlparse.urlparse(url)
        self.remote_host = parts[1]
        self.secure = parts[0] == 'https'

    def callRemote(self, method, *args):
        factory = xmlrpc.QueryFactory(self.url, self.remote_host, method, *args)
        if self.secure:
            from twisted.internet import ssl
            self.reactor.connectSSL(self.proxy_host, self.proxy_port,
                               factory, ssl.ClientContextFactory())
            self.reactor.connectTCP(self.proxy_host, self.proxy_port, factory)
        return factory.deferred



More information about the Twisted-Python mailing list