expose more info when certificate check fails
[nodemanager.git] / safexmlrpc.py
1 """Leverage curl to make XMLRPC requests that check the server's credentials."""
2
3 import curlwrapper
4 import xmlrpclib
5
6
7 class CertificateCheckingSafeTransport(xmlrpclib.Transport):
8     def __init__(self, cacert, timeout):
9         self.cacert = cacert
10         self.timeout = timeout
11
12     def request(self, host, handler, request_body, verbose=0):
13         self.verbose = verbose
14         url='https://%s%s' % (host, handler)
15         try:
16             contents = curlwrapper.retrieve(url,
17                                             cacert = self.cacert,
18                                             postdata = request_body,
19                                             timeout = self.timeout)
20             return xmlrpclib.loads(contents)[0]
21         except curlwrapper.CurlException, e:
22             # when this triggers, the error sometimes doesn't get printed
23             print 'CertificateCheckingSafeTransport.request: Catching curlwrapper.CurlException with error <%s>'%str(e)
24             raise xmlrpclib.ProtocolError(url, -1, str(e), request_body)
25
26 class ServerProxy(xmlrpclib.ServerProxy):
27     def __init__(self, uri, cacert, timeout = 300, **kwds):
28         xmlrpclib.ServerProxy.__init__(self, uri,
29                                        CertificateCheckingSafeTransport(cacert, timeout),
30                                        **kwds)