fix timeout bug for python 2.6
authorTony Mack <tmack@paris.CS.Princeton.EDU>
Thu, 4 Aug 2011 23:57:43 +0000 (19:57 -0400)
committerTony Mack <tmack@paris.CS.Princeton.EDU>
Thu, 4 Aug 2011 23:57:43 +0000 (19:57 -0400)
sfa/util/httpsProtocol.py
sfa/util/xmlrpcprotocol.py

index a9d86ad..0766acf 100644 (file)
@@ -1,5 +1,10 @@
 import httplib
 import socket
+import sys
+
+
+def is_python26():
+    return sys.version_info[0] == 2 and sys.version_info[1] == 6
 
 # wrapper around standartd https modules. Properly supports timeouts.  
 
@@ -11,14 +16,22 @@ class HTTPSConnection(httplib.HTTPSConnection):
 
     def connect(self):
         """Connect to a host on a given (SSL) port."""
-        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        sock.settimeout(self.timeout)
-        sock.connect((self.host, self.port))
-        ssl = socket.ssl(sock, self.key_file, self.cert_file)
-        self.sock = httplib.FakeSocket(sock, ssl)
+        if is_python26():
+            from sfa.util.ssl_socket import SSLSocket
+            sock = socket.create_connection((self.host, self.port), self.timeout)
+            if self._tunnel_host:
+                self.sock = sock
+                self._tunnel()
+            self.sock = SSLSocket(sock, self.key_file, self.cert_file)
+        else:
+            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+            sock.settimeout(self.timeout)
+            sock.connect((self.host, self.port))
+            ssl = socket.ssl(sock, self.key_file, self.cert_file)
+            self.sock = httplib.FakeSocket(sock, ssl)
 
 class HTTPS(httplib.HTTPS):
-   def __init__(self, host='', port=None, key_file=None, cert_file=None,
+    def __init__(self, host='', port=None, key_file=None, cert_file=None,
                      strict=None, timeout = None):
         # urf. compensate for bad input.
         if port == 0:
@@ -29,4 +42,7 @@ class HTTPS(httplib.HTTPS):
         # here for compatibility with post-1.5.2 CVS.
         self.key_file = key_file
         self.cert_file = cert_file
-
+    
+    def set_timeout(self, timeout):
+        if is_python26():
+            self._conn.timeout = timeout
index c321406..6bab4c6 100644 (file)
@@ -46,9 +46,14 @@ class XMLRPCTransport(xmlrpclib.Transport):
         # host may be a string, or a (host, x509-dict) tuple
         host, extra_headers, x509 = self.get_host_info(host)
         if need_HTTPSConnection:
-            return 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, timeout=self.timeout) #**(x509 or {}))
         else:
-            return 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, timeout=self.timeout) #**(x509 or {}))
+
+        if hasattr(conn, 'set_timeout'):
+            conn.set_timeout(self.timeout)
+
+        return conn
 
     def getparser(self):
         unmarshaller = ExceptionUnmarshaller()