xmlrpc, httplib and SSL (HTTP 1.1 XMLRPC client)

Etienne Posthumus ep at epoz.org
Thu Apr 1 06:54:38 EST 2004


On Mar 27, 2004, at 5:03 AM, Roger Binns wrote:
> However I was wondering if anyone was working on fixing the
> (IMHO horrible) mess and wants any moral support?

I wanted to do keep the connections on which my clients do XMLRPC calls 
open, and after staring at the xmlrpclib.py source for a while, came up 
with the class at the bottom of the message. Just thought I would post 
it here in the spirit of sharing. You would use it like this:

import httplib, xmlrpclib
s = xmlrpclib.ServerProxy('http://SOMEURL', 
transport=PersistTransport())

And then use as normal. When any error occurs, the connection is 
closed, seems a bit pessimistic, but I didn't want to do anything more 
fancy.

cheers,

Etienne Posthumus
---
http://www.mnemosyne.org/
Cultural Heritage Research
Python, Zope, XML expertise for hire.
Amsterdam, Nederland
----

class PersistTransport(xmlrpclib.Transport):
     '''Provides a Transport for the xmlrpclib that uses httplib 
supporting persistent connections
     Does not close the connection after each request.
     '''
     connection = None

     def request(self, host, handler, request_body, verbose=0):
         if not self.connection:
             host, extra_headers, x509 = self.get_host_info(host)
             self.connection = httplib.HTTPConnection(host)
             self.headers = {"User-Agent" : self.user_agent,
                "Content-Type" : "text/xml",
                "Accept": "text/xml"}
             if extra_headers:
                 for key, item in extra_headers:
                     self.headers[key] = item

         self.headers["Content-Length"] = str(len(request_body))
         self.connection.request('POST', handler, request_body, 
self.headers)
         r = self.connection.getresponse()
         if r.status != 200:
             self.connection.close()
             self.connection = None
             raise xmlrpclib.ProtocolError( host + handler, r.status, 
r.reason, '' )
         data = r.read()
         p, u = self.getparser()
         p.feed(data)
         p.close()
         return u.close()





More information about the Python-list mailing list