public
Last active

Python xmlrpc lib Transport that used Requests

  • Download Gist
transport.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
# -*- coding: utf-8 -*-
"""
A replacement transport for Python xmlrpc library.
 
Usage:
 
>>> import xmlrpclib
>>> from transport import RequestsTransport
>>> s = xmlrpclib.ServerProxy('http://yoursite.com/xmlrpc', transport=RequestsTransport())
>>> s.demo.sayHello()
Hello!
"""
try:
import xmlrpc.client as xmlrpc
except ImportError:
import xmlrpclib as xmlrpc
 
import requests
 
class RequestsTransport(xmlrpc.Transport):
"""
Drop in Transport for xmlrpclib that uses Requests instead of httplib
"""
# change our user agent to reflect Requests
user_agent = "Python XMLRPC with Requests (python-requests.org)"
 
# override this if you'd like to https
use_https = False
 
def request(self, host, handler, request_body, verbose):
"""
Make an xmlrpc request.
"""
headers = {'User-Agent': self.user_agent}
url = self._build_url(host, handler)
try:
resp = requests.post(url, data=request_body, headers=headers)
except ValueError:
raise
except Exception:
raise # something went wrong
else:
try:
resp.raise_for_status()
except requests.RequestException as e:
raise xmlrpc.ProtocolError(url, resp.status_code,
str(e), resp.headers)
else:
return self.parse_response(resp)
 
def parse_response(self, resp):
"""
Parse the xmlrpc response.
"""
p, u = self.getparser()
p.feed(resp.text)
p.close()
return u.close()
 
def _build_url(self, host, handler):
"""
Build a url for our request based on the host, handler and use_http
property
"""
scheme = 'https' if self.use_https else 'http'
return '%s://%s/%s' % (scheme, host, handler)

To use with Plone, set the content type header to "text/xml". For Basic Auth support, get_host_info (http://hg.python.org/cpython/file/2.7/Lib/xmlrpclib.py#l1334) is your friend for getting the user & passwd to pass to requests.post.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.