reguire gnupg1 on f>=31; sense the system to use gpg1 when installed
[nodemanager.git] / safexmlrpc.py
index 96865df..71bf4d6 100644 (file)
@@ -1,24 +1,29 @@
 """Leverage curl to make XMLRPC requests that check the server's credentials."""
 
-from subprocess import PIPE, Popen
-import xmlrpclib
+import xmlrpc.client
 
+import curlwrapper
 
-CURL = '/usr/bin/curl'
 
-class CertificateCheckingSafeTransport(xmlrpclib.Transport):
+class CertificateCheckingSafeTransport (xmlrpc.client.Transport):
+
+    def __init__(self, cacert, timeout):
+        self.cacert = cacert
+        self.timeout = timeout
+
     def request(self, host, handler, request_body, verbose=0):
         self.verbose = verbose
-        p = Popen((CURL, '--cacert', '/usr/boot/cacert.pem', '--data', '@-', 'https://%s%s' % (host, handler)), stdin=PIPE, stdout=PIPE, stderr=PIPE)
-        p.stdin.write(request_body)
-        p.stdin.close()
-        contents = p.stdout.read()
-        p.stdout.close()
-        error = p.stderr.read()
-        p.stderr.close()
-        rc = p.wait()
-        if rc != 0: raise xmlrpclib.ProtocolError(host + handler, rc, error, '')
-        return xmlrpclib.loads(contents)[0]
-
-class ServerProxy(xmlrpclib.ServerProxy):
-    def __init__(self, handler, *args, **kw_args): xmlrpclib.ServerProxy.__init__(self, handler, CertificateCheckingSafeTransport())
+        url='https://%s%s' % (host, handler)
+        # this might raise an xmlrpclib.Protocolerror exception
+        contents = curlwrapper.retrieve(url,
+                                        cacert = self.cacert,
+                                        postdata = request_body,
+                                        timeout = self.timeout)
+        return xmlrpc.client.loads(contents)[0]
+
+class ServerProxy(xmlrpc.client.ServerProxy):
+
+    def __init__(self, uri, cacert, timeout = 300, **kwds):
+        xmlrpc.client.ServerProxy.__init__(self, uri,
+                                       CertificateCheckingSafeTransport(cacert, timeout),
+                                       **kwds)