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
12 from tempfile import NamedTemporaryFile
14 class PyCurlTransport(xmlrpclib.Transport):
15 def __init__(self, uri, cert = None, timeout = 300):
16 if hasattr(xmlrpclib.Transport,'__init__'):
17 xmlrpclib.Transport.__init__(self)
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 errmsg="<no known errmsg>"
68 except pycurl.error, err:
69 (errcode, errmsg) = err
72 raise Exception, "PyCurl: SSL certificate validation failed"
74 raise Exception, "PyCurl: HTTP error %d -- %r" % (errcode,errmsg)
77 p, u = self.getparser()