Transport class for Python's XML-RPC lib

Given that the xmlrpclib.Transport class can be derived, it is perhaps easier to define a new transport class that implements the patch shown in the facelift post, though I still believe Python's XML-RPC library is due a much needed update.

Thus, I present the HTTPTransport class:

Update: It seems I forgot to parse the resulting payload. this is now fixed in the updated code below.

from xmlrpclib import Transport
from xmlrpclib import ProtocolError

class HTTPTransport(Transport):
##
# Connect to server.
#
# @param host Target host.
# @return A connection handle.

def make_connection(self, host):
# create a HTTP connection object from a host descriptor
import httplib
host, extra_headers, x509 = self.get_host_info(host)
return httplib.HTTPConnection(host)
##
# Send a complete request, and parse the response.
#
# @param host Target host.
# @param handler Target PRC handler.
# @param request_body XML-RPC request body.
# @param verbose Debugging flag.
# @return XML response.

def request(self, host, handler, request_body, verbose=0):
# issue XML-RPC request

h = self.make_connection(host)
if verbose:
h.set_debuglevel(1)

self.send_request(h, handler, request_body)
self.send_host(h, host)
self.send_user_agent(h)
self.send_content(h, request_body)

response = h.getresponse()

if response.status != 200:
raise ProtocolError(host + handler, response.status, response.reason, response.msg.headers)

payload = response.read()
parser, unmarshaller = self.getparser()
parser.feed(payload)
parser.close()

return unmarshaller.close()

Reply

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options