Skip to content

Instantly share code, notes, and snippets.

@xordoquy
Created January 7, 2014 09:22
Show Gist options
  • Save xordoquy/8296853 to your computer and use it in GitHub Desktop.
Save xordoquy/8296853 to your computer and use it in GitHub Desktop.
This is a transport layer in order to make xmlrpc work with any proxy type thanks to the requests library
class RequestTransport(xmlrpclib.Transport, object):
def __init__(self, use_datetime=0, proxies=None):
super(RequestTransport, self).__init__(use_datetime)
self.session = requests.Session()
self.configure_requests(proxies=proxies)
def configure_requests(self, proxies=None):
self.session.headers.update({
'Content-Type': 'text/xml',
'User-Agent': self.user_agent,
'Accept-Encoding': 'identity',
})
self.session.proxies = copy(proxies)
def set_proxy(self, proxies):
self.session.proxies = copy(proxies)
def request(self, host, handler, request_body, verbose=0):
r = self.session.post('https://%s%s' % (host, handler), data=request_body)
if r.status_code == 200:
self.verbose = verbose
from StringIO import StringIO
s = StringIO(r.content)
return self.parse_response(s)
# Usage:
proxy = RequestTransport()
proxy.set_proxy({
'http': 'http://localhost:8080',
'https': 'http://localhost:8080',
})
client = xmlrpclib.ServerProxy('https://pypi.python.org/pypi', transport=proxy)
reponse = client.package_releases('Django', True)
print(response)
print(response.content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment