2to3 -f except
[sfa.git] / sfa / client / sfaserverproxy.py
index 18b7713..ca62ddf 100644 (file)
@@ -1,5 +1,10 @@
 # XMLRPC-specific code for SFA Client
 
+# starting with 2.7.9 we need to turn off server verification
+import ssl
+try:    turn_off_server_verify = { 'context' : ssl._create_unverified_context() } 
+except: turn_off_server_verify = {}
+
 import xmlrpclib
 from httplib import HTTPS, HTTPSConnection
 
@@ -7,7 +12,7 @@ try:
     from sfa.util.sfalogging import logger
 except:
     import logging
-    logger=logging.getLogger('sfaserverproxy')
+    logger = logging.getLogger('sfaserverproxy')
 
 ##
 # ServerException, ExceptionUnmarshaller
@@ -22,7 +27,7 @@ class ExceptionUnmarshaller(xmlrpclib.Unmarshaller):
     def close(self):
         try:
             return xmlrpclib.Unmarshaller.close(self)
-        except xmlrpclib.Fault, e:
+        except xmlrpclib.Fault as e:
             raise ServerException(e.faultString)
 
 ##
@@ -30,17 +35,11 @@ class ExceptionUnmarshaller(xmlrpclib.Unmarshaller):
 #
 # A transport for XMLRPC that works on top of HTTPS
 
-# python 2.7 xmlrpclib has changed its internal code
-# it now calls 'getresponse' on the obj returned by make_connection
-# while it used to call 'getreply'
-# regardless of the version, httplib.HTTPS does implement getreply, 
-# while httplib.HTTPSConnection has getresponse
-# so we create a dummy instance to check what's expected
-need_HTTPSConnection=hasattr(xmlrpclib.Transport().make_connection('localhost'),'getresponse')
+# targetting only python-2.7 we can get rid of some older code
 
 class XMLRPCTransport(xmlrpclib.Transport):
     
-    def __init__(self, key_file=None, cert_file=None, timeout=None):
+    def __init__(self, key_file = None, cert_file = None, timeout = None):
         xmlrpclib.Transport.__init__(self)
         self.timeout=timeout
         self.key_file = key_file
@@ -50,21 +49,18 @@ class XMLRPCTransport(xmlrpclib.Transport):
         # create a HTTPS connection object from a host descriptor
         # host may be a string, or a (host, x509-dict) tuple
         host, extra_headers, x509 = self.get_host_info(host)
-        if need_HTTPSConnection:
-            #conn = HTTPSConnection(host, None, key_file=self.key_file, cert_file=self.cert_file, timeout=self.timeout) #**(x509 or {}))
-            conn = HTTPSConnection(host, None, key_file=self.key_file, cert_file=self.cert_file) #**(x509 or {}))
-        else:
-            #conn = HTTPS(host, None, key_file=self.key_file, cert_file=self.cert_file, timeout=self.timeout) #**(x509 or {}))
-            conn = HTTPS(host, None, key_file=self.key_file, cert_file=self.cert_file) #**(x509 or {}))
-
-        if hasattr(conn, 'set_timeout'):
-            conn.set_timeout(self.timeout)
+        conn = HTTPSConnection(host, None, key_file = self.key_file,
+                               cert_file = self.cert_file,
+                               **turn_off_server_verify)
 
         # Some logic to deal with timeouts. It appears that some (or all) versions
         # of python don't set the timeout after the socket is created. We'll do it
         # ourselves by forcing the connection to connect, finding the socket, and
         # calling settimeout() on it. (tested with python 2.6)
         if self.timeout:
+            if hasattr(conn, 'set_timeout'):
+                conn.set_timeout(self.timeout)
+
             if hasattr(conn, "_conn"):
                 # HTTPS is a wrapper around HTTPSConnection
                 real_conn = conn._conn
@@ -84,22 +80,25 @@ class XMLRPCTransport(xmlrpclib.Transport):
 class XMLRPCServerProxy(xmlrpclib.ServerProxy):
     def __init__(self, url, transport, allow_none=True, verbose=False):
         # remember url for GetVersion
+        # xxx not sure this is still needed as SfaServerProxy has this too
         self.url=url
-        xmlrpclib.ServerProxy.__init__(self, url, transport, allow_none=allow_none, verbose=verbose)
+        xmlrpclib.ServerProxy.__init__(self, url, transport, allow_none=allow_none,
+                                       verbose=verbose,
+                                       **turn_off_server_verify)
 
     def __getattr__(self, attr):
-        logger.debug ("xml-rpc %s method:%s"%(self.url,attr))
+        logger.debug ("xml-rpc %s method:%s" % (self.url, attr))
         return xmlrpclib.ServerProxy.__getattr__(self, attr)
 
 ########## the object on which we can send methods that get sent over xmlrpc
 class SfaServerProxy:
 
     def __init__ (self, url, keyfile, certfile, verbose=False, timeout=None):
-        self.url=url
-        self.keyfile=keyfile
-        self.certfile=certfile
-        self.verbose=verbose
-        self.timeout=timeout
+        self.url = url
+        self.keyfile = keyfile
+        self.certfile = certfile
+        self.verbose = verbose
+        self.timeout = timeout
         # an instance of xmlrpclib.ServerProxy
         transport = XMLRPCTransport(keyfile, certfile, timeout)
         self.serverproxy = XMLRPCServerProxy(url, transport, allow_none=True, verbose=verbose)