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
14 from tempfile import NamedTemporaryFile
16 class PyCurlTransport(xmlrpclib.Transport):
17 def __init__(self, uri, cert = None, timeout = 300):
18 self.curl = pycurl.Curl()
21 self.curl.setopt(pycurl.NOSIGNAL, 1)
24 self.curl.setopt(pycurl.FOLLOWLOCATION, 1)
28 self.curl.setopt(pycurl.URL, str(uri))
30 # Set certificate path
32 if os.path.exists(cert):
35 # Keep a reference so that it does not get deleted
36 self.cert = NamedTemporaryFile(prefix = "cert")
39 cert_path = self.cert.name
40 self.curl.setopt(pycurl.CAINFO, cert_path)
41 self.curl.setopt(pycurl.SSL_VERIFYPEER, 2)
43 # Set connection timeout
45 self.curl.setopt(pycurl.CONNECTTIMEOUT, timeout)
46 self.curl.setopt(pycurl.TIMEOUT, timeout)
48 # Set request callback
52 self.curl.setopt(pycurl.WRITEFUNCTION, body)
54 def request(self, host, handler, request_body, verbose = 1):
56 self.curl.setopt(pycurl.VERBOSE, verbose)
59 self.curl.setopt(pycurl.POST, 1)
60 self.curl.setopt(pycurl.POSTFIELDS, request_body)
64 errcode = self.curl.getinfo(pycurl.HTTP_CODE)
67 except pycurl.error, err:
68 (errcode, errmsg) = err
71 raise Exception, "SSL certificate validation failed"
73 raise Exception, "HTTP error %d" % errcode
76 p, u = self.getparser()