def connect(self): """Create SSL socket and connect to peer """ if getattr(self, 'ssl_context', None): if not isinstance(self.ssl_context, SSL.Context): raise TypeError('Expecting OpenSSL.SSL.Context type for "' 'ssl_context" attribute; got %r instead' % self.ssl_context) ssl_context = self.ssl_context else: ssl_context = SSL.Context(self.__class__.default_ssl_method) sock = socket.create_connection((self.host, self.port), self.timeout) # Tunnel if using a proxy - ONLY available for Python 2.6.2 and above if getattr(self, '_tunnel_host', None): self.sock = sock self._tunnel() self.sock = SSLSocket(ssl_context, sock) # Go to client mode. self.sock.set_connect_state()