def __init__(self, sock, keyfile=None, certfile=None): socket.__init__(self, _sock=sock._sock) # the initializer for socket trashes the methods (tsk, tsk), so... self.send = lambda data, flags=0: SSLSocket.send(self, data, flags) self.sendto = lambda data, addr, flags=0: SSLSocket.sendto(self, data, addr, flags) self.recv = lambda buflen=1024, flags=0: SSLSocket.recv(self, buflen, flags) self.recvfrom = lambda addr, buflen=1024, flags=0: SSLSocket.recvfrom(self, addr, buflen, flags) self.recv_into = lambda buffer, nbytes=None, flags=0: SSLSocket.recv_into(self, buffer, nbytes, flags) self.recvfrom_into = lambda buffer, nbytes=None, flags=0: SSLSocket.recvfrom_into(self, buffer, nbytes, flags) if certfile and not keyfile: keyfile = certfile # see if it's connected try: socket.getpeername(self) except socket_error: # no, no connection yet self._sslobj = None else: # yes, create the SSL object self._sslobj = _ssl.ssl(self._sock, keyfile, certfile) timeout = self.gettimeout() try: self.settimeout(None) self.do_handshake() finally: self.settimeout(timeout) self.keyfile = keyfile self.certfile = certfile self._makefile_refs = 0
def connect(self, addr): """Connects to remote ADDR, and then wraps the connection in an SSL channel.""" # Here we assume that the socket is client-side, and not # connected at the time of the call. We connect it, then wrap it. if self._sslobj: raise ValueError("attempt to connect already-connected SSLSocket!") socket.connect(self, addr) self._sslobj = _ssl.ssl(self._sock, self.keyfile, self.certfile) self.do_handshake()