clean up code for python < 2.7
Thierry Parmentelat [Tue, 17 Mar 2015 13:42:21 +0000 (14:42 +0100)]
sfa/client/sfaserverproxy.py

index 762ea38..615a8b6 100644 (file)
@@ -11,7 +11,7 @@ try:
     from sfa.util.sfalogging import logger
 except:
     import logging
-    logger=logging.getLogger('sfaserverproxy')
+    logger = logging.getLogger('sfaserverproxy')
 
 ##
 # ServerException, ExceptionUnmarshaller
@@ -34,17 +34,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
@@ -54,16 +48,13 @@ 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:
-            if not ssl_needs_unverified_context:
-                conn = HTTPSConnection(host, None, key_file = self.key_file,
-                                       cert_file = self.cert_file)
-            else:
-                conn = HTTPSConnection(host, None, key_file = self.key_file,
-                                       cert_file = self.cert_file,
-                                       context = ssl._create_unverified_context())
+        if not ssl_needs_unverified_context:
+            conn = HTTPSConnection(host, None, key_file = self.key_file,
+                                   cert_file = self.cert_file)
         else:
-            conn = HTTPS(host, None, key_file=self.key_file, cert_file=self.cert_file)
+            conn = HTTPSConnection(host, None, key_file = self.key_file,
+                                   cert_file = self.cert_file,
+                                   context = ssl._create_unverified_context())
 
         # 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
@@ -110,11 +101,11 @@ class XMLRPCServerProxy(xmlrpclib.ServerProxy):
 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)