From: Tony Mack Date: Thu, 4 Aug 2011 23:57:43 +0000 (-0400) Subject: fix timeout bug for python 2.6 X-Git-Tag: sfa-1.0-29~2^2 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=7d2b8f43411d4ecd73f64cd25e6f7276712de4dc;p=sfa.git fix timeout bug for python 2.6 --- diff --git a/sfa/util/httpsProtocol.py b/sfa/util/httpsProtocol.py index a9d86adb..0766acff 100644 --- a/sfa/util/httpsProtocol.py +++ b/sfa/util/httpsProtocol.py @@ -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 diff --git a/sfa/util/xmlrpcprotocol.py b/sfa/util/xmlrpcprotocol.py index c321406c..6bab4c63 100644 --- a/sfa/util/xmlrpcprotocol.py +++ b/sfa/util/xmlrpcprotocol.py @@ -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()