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 if hasattr(xmlrpclib.Transport,'__init__'):
19 xmlrpclib.Transport.__init__(self)
20 self.curl = pycurl.Curl()
23 self.curl.setopt(pycurl.NOSIGNAL, 1)
26 self.curl.setopt(pycurl.FOLLOWLOCATION, 1)
30 self.curl.setopt(pycurl.URL, str(uri))
32 # Set certificate path
34 if os.path.exists(cert):
37 # Keep a reference so that it does not get deleted
38 self.cert = NamedTemporaryFile(prefix = "cert")
41 cert_path = self.cert.name
42 self.curl.setopt(pycurl.CAINFO, cert_path)
43 self.curl.setopt(pycurl.SSL_VERIFYPEER, 2)
45 # Set connection timeout
47 self.curl.setopt(pycurl.CONNECTTIMEOUT, timeout)
48 self.curl.setopt(pycurl.TIMEOUT, timeout)
50 # Set request callback
54 self.curl.setopt(pycurl.WRITEFUNCTION, body)
56 def request(self, host, handler, request_body, verbose = 1):
58 self.curl.setopt(pycurl.VERBOSE, verbose)
61 self.curl.setopt(pycurl.POST, 1)
62 self.curl.setopt(pycurl.POSTFIELDS, request_body)
66 errcode = self.curl.getinfo(pycurl.HTTP_CODE)
69 errmsg="<no known errmsg>"
70 except pycurl.error, err:
71 (errcode, errmsg) = err
74 raise Exception, "PyCurl: SSL certificate validation failed"
76 raise Exception, "PyCurl: HTTP error %d -- %r" % (errcode,errmsg)
79 p, u = self.getparser()