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.
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:
# 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
# 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()