2 # Replacement for xmlrpclib.SafeTransport, which does not validate
3 # SSL certificates. Requires PyCurl.
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2006 The Trustees of Princeton University
15 from tempfile import NamedTemporaryFile
17 class PyCurlTransport(xmlrpclib.Transport):
18 def __init__(self, uri, cert = None, timeout = 300):
19 if hasattr(xmlrpclib.Transport,'__init__'):
20 xmlrpclib.Transport.__init__(self)
21 self.curl = pycurl.Curl()
24 self.curl.setopt(pycurl.NOSIGNAL, 1)
27 self.curl.setopt(pycurl.FOLLOWLOCATION, 1)
31 self.curl.setopt(pycurl.URL, str(uri))
33 # Set certificate path
35 if os.path.exists(cert):
38 # Keep a reference so that it does not get deleted
39 self.cert = NamedTemporaryFile(prefix = "cert")
42 cert_path = self.cert.name
43 self.curl.setopt(pycurl.CAINFO, cert_path)
44 self.curl.setopt(pycurl.SSL_VERIFYPEER, 2)
46 # Set connection timeout
48 self.curl.setopt(pycurl.CONNECTTIMEOUT, timeout)
49 self.curl.setopt(pycurl.TIMEOUT, timeout)
51 # Set request callback
55 self.curl.setopt(pycurl.WRITEFUNCTION, body)
57 def request(self, host, handler, request_body, verbose = 1):
59 self.curl.setopt(pycurl.VERBOSE, verbose)
62 self.curl.setopt(pycurl.POST, 1)
63 self.curl.setopt(pycurl.POSTFIELDS, request_body)
67 errcode = self.curl.getinfo(pycurl.HTTP_CODE)
70 errmsg="<no known errmsg>"
71 except pycurl.error, err:
72 (errcode, errmsg) = err
75 raise Exception, "PyCurl: SSL certificate validation failed"
77 raise Exception, "PyCurl: HTTP error %d -- %r" % (errcode,errmsg)
80 p, u = self.getparser()