Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
ssl.SSLSocket Class Reference
Inheritance diagram for ssl.SSLSocket:
Inheritance graph
[legend]
Collaboration diagram for ssl.SSLSocket:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def dup
def read
def write
def getpeercert
def cipher
def send
def sendto
def sendall
def recv
def recv_into
def recvfrom
def recvfrom_into
def pending
def shutdown
def unwrap
def do_handshake
def connect
def connect_ex
def accept
def __del__
def __enter__
def __exit__
def __repr__
def makefile
def close

Public Attributes

 context
 keyfile
 certfile
 cert_reqs
 ssl_version
 ca_certs
 ciphers
 server_side
 server_hostname
 do_handshake_on_connect
 suppress_ragged_eofs

Private Member Functions

def _checkClosed
def _real_close
def _real_connect

Private Attributes

 _closed
 _sslobj
 _connected

Detailed Description

This class implements a subtype of socket.socket that wraps
the underlying OS socket in an SSL context when necessary, and
provides read and write methods over that channel.

Definition at line 184 of file ssl.py.


Constructor & Destructor Documentation

def ssl.SSLSocket.__init__ (   self,
  sock = None,
  keyfile = None,
  certfile = None,
  server_side = False,
  cert_reqs = CERT_NONE,
  ssl_version = PROTOCOL_SSLv23,
  ca_certs = None,
  do_handshake_on_connect = True,
  family = AF_INET,
  type = SOCK_STREAM,
  proto = 0,
  fileno = None,
  suppress_ragged_eofs = True,
  ciphers = None,
  server_hostname = None,
  _context = None 
)

Definition at line 196 of file ssl.py.

00196 
00197                  _context=None):
00198 
00199         if _context:
00200             self.context = _context
00201         else:
00202             if server_side and not certfile:
00203                 raise ValueError("certfile must be specified for server-side "
00204                                  "operations")
00205             if keyfile and not certfile:
00206                 raise ValueError("certfile must be specified")
00207             if certfile and not keyfile:
00208                 keyfile = certfile
00209             self.context = SSLContext(ssl_version)
00210             self.context.verify_mode = cert_reqs
00211             if ca_certs:
00212                 self.context.load_verify_locations(ca_certs)
00213             if certfile:
00214                 self.context.load_cert_chain(certfile, keyfile)
00215             if ciphers:
00216                 self.context.set_ciphers(ciphers)
00217             self.keyfile = keyfile
00218             self.certfile = certfile
00219             self.cert_reqs = cert_reqs
00220             self.ssl_version = ssl_version
00221             self.ca_certs = ca_certs
00222             self.ciphers = ciphers
00223         if server_side and server_hostname:
00224             raise ValueError("server_hostname can only be specified "
00225                              "in client mode")
00226         self.server_side = server_side
00227         self.server_hostname = server_hostname
00228         self.do_handshake_on_connect = do_handshake_on_connect
00229         self.suppress_ragged_eofs = suppress_ragged_eofs
00230         connected = False
00231         if sock is not None:
00232             socket.__init__(self,
00233                             family=sock.family,
00234                             type=sock.type,
00235                             proto=sock.proto,
00236                             fileno=sock.fileno())
00237             self.settimeout(sock.gettimeout())
00238             # see if it's connected
00239             try:
00240                 sock.getpeername()
00241             except socket_error as e:
00242                 if e.errno != errno.ENOTCONN:
00243                     raise
00244             else:
00245                 connected = True
00246             sock.detach()
00247         elif fileno is not None:
00248             socket.__init__(self, fileno=fileno)
00249         else:
00250             socket.__init__(self, family=family, type=type, proto=proto)
00251 
00252         self._closed = False
00253         self._sslobj = None
00254         self._connected = connected
00255         if connected:
00256             # create the SSL object
00257             try:
00258                 self._sslobj = self.context._wrap_socket(self, server_side,
00259                                                          server_hostname)
00260                 if do_handshake_on_connect:
00261                     timeout = self.gettimeout()
00262                     if timeout == 0.0:
00263                         # non-blocking
00264                         raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
00265                     self.do_handshake()
00266 
00267             except socket_error as x:
00268                 self.close()
00269                 raise x

Here is the caller graph for this function:

def ssl.SSLSocket.__del__ (   self)

Definition at line 497 of file ssl.py.

00497 
00498     def __del__(self):
00499         # sys.stderr.write("__del__ on %s\n" % repr(self))
00500         self._real_close()
00501 

Here is the call graph for this function:


Member Function Documentation

def socket.socket.__enter__ (   self) [inherited]

Definition at line 98 of file socket.py.

00098 
00099     def __enter__(self):
00100         return self

def socket.socket.__exit__ (   self,
  args 
) [inherited]

Definition at line 101 of file socket.py.

00101 
00102     def __exit__(self, *args):
00103         if not self._closed:
00104             self.close()

Here is the caller graph for this function:

def socket.socket.__repr__ (   self) [inherited]
Wrap __repr__() to reveal the real class name.

Definition at line 105 of file socket.py.

00105 
00106     def __repr__(self):
00107         """Wrap __repr__() to reveal the real class name."""
00108         s = _socket.socket.__repr__(self)
00109         if s.startswith("<socket object"):
00110             s = "<%s.%s%s%s" % (self.__class__.__module__,
00111                                 self.__class__.__name__,
00112                                 getattr(self, '_closed', False) and " [closed] " or "",
00113                                 s[7:])
00114         return s

Here is the call graph for this function:

def ssl.SSLSocket._checkClosed (   self,
  msg = None 
) [private]

Definition at line 274 of file ssl.py.

00274 
00275     def _checkClosed(self, msg=None):
00276         # raise an exception here if you wish to check for spurious closes
00277         pass

Here is the caller graph for this function:

def ssl.SSLSocket._real_close (   self) [private]

Definition at line 431 of file ssl.py.

00431 
00432     def _real_close(self):
00433         self._sslobj = None
00434         # self._closed = True
00435         socket._real_close(self)

def ssl.SSLSocket._real_connect (   self,
  addr,
  connect_ex 
) [private]

Definition at line 447 of file ssl.py.

00447 
00448     def _real_connect(self, addr, connect_ex):
00449         if self.server_side:
00450             raise ValueError("can't connect in server-side mode")
00451         # Here we assume that the socket is client-side, and not
00452         # connected at the time of the call.  We connect it, then wrap it.
00453         if self._connected:
00454             raise ValueError("attempt to connect already-connected SSLSocket!")
00455         self._sslobj = self.context._wrap_socket(self, False, self.server_hostname)
00456         try:
00457             if connect_ex:
00458                 rc = socket.connect_ex(self, addr)
00459             else:
00460                 rc = None
00461                 socket.connect(self, addr)
00462             if not rc:
00463                 if self.do_handshake_on_connect:
00464                     self.do_handshake()
00465                 self._connected = True
00466             return rc
00467         except socket_error:
00468             self._sslobj = None
00469             raise

Here is the call graph for this function:

Here is the caller graph for this function:

def ssl.SSLSocket.accept (   self)
Accepts a new connection from a remote client, and returns
a tuple containing that new connection wrapped with a server-side
SSL channel, and the address of the remote client.

Reimplemented from socket.socket.

Definition at line 480 of file ssl.py.

00480 
00481     def accept(self):
00482         """Accepts a new connection from a remote client, and returns
00483         a tuple containing that new connection wrapped with a server-side
00484         SSL channel, and the address of the remote client."""
00485 
00486         newsock, addr = socket.accept(self)
00487         return (SSLSocket(sock=newsock,
00488                           keyfile=self.keyfile, certfile=self.certfile,
00489                           server_side=True,
00490                           cert_reqs=self.cert_reqs,
00491                           ssl_version=self.ssl_version,
00492                           ca_certs=self.ca_certs,
00493                           ciphers=self.ciphers,
00494                           do_handshake_on_connect=
00495                               self.do_handshake_on_connect),
00496                 addr)

def ssl.SSLSocket.cipher (   self)

Definition at line 314 of file ssl.py.

00314 
00315     def cipher(self):
00316         self._checkClosed()
00317         if not self._sslobj:
00318             return None
00319         else:
00320             return self._sslobj.cipher()

Here is the call graph for this function:

def socket.socket.close (   self) [inherited]

Definition at line 194 of file socket.py.

00194 
00195     def close(self):
00196         # This function should not reference any globals. See issue #808164.
00197         self._closed = True
00198         if self._io_refs <= 0:
00199             self._real_close()

Here is the call graph for this function:

Here is the caller graph for this function:

def ssl.SSLSocket.connect (   self,
  addr 
)
Connects to remote ADDR, and then wraps the connection in
an SSL channel.

Definition at line 470 of file ssl.py.

00470 
00471     def connect(self, addr):
00472         """Connects to remote ADDR, and then wraps the connection in
00473         an SSL channel."""
00474         self._real_connect(addr, False)

Here is the call graph for this function:

Here is the caller graph for this function:

def ssl.SSLSocket.connect_ex (   self,
  addr 
)
Connects to remote ADDR, and then wraps the connection in
an SSL channel.

Definition at line 475 of file ssl.py.

00475 
00476     def connect_ex(self, addr):
00477         """Connects to remote ADDR, and then wraps the connection in
00478         an SSL channel."""
00479         return self._real_connect(addr, True)

Here is the call graph for this function:

def ssl.SSLSocket.do_handshake (   self,
  block = False 
)
Perform a TLS/SSL handshake.

Definition at line 436 of file ssl.py.

00436 
00437     def do_handshake(self, block=False):
00438         """Perform a TLS/SSL handshake."""
00439 
00440         timeout = self.gettimeout()
00441         try:
00442             if timeout == 0.0 and block:
00443                 self.settimeout(None)
00444             self._sslobj.do_handshake()
00445         finally:
00446             self.settimeout(timeout)

Here is the call graph for this function:

Here is the caller graph for this function:

def ssl.SSLSocket.dup (   self)
dup() -> socket object

Return a new socket object connected to the same system resource.

Reimplemented from socket.socket.

Definition at line 270 of file ssl.py.

00270 
00271     def dup(self):
00272         raise NotImplemented("Can't dup() %s instances" %
00273                              self.__class__.__name__)

Here is the caller graph for this function:

def ssl.SSLSocket.getpeercert (   self,
  binary_form = False 
)
Returns a formatted version of the data in the
certificate provided by the other end of the SSL channel.
Return None if no certificate was provided, {} if a
certificate was provided, but not validated.

Definition at line 305 of file ssl.py.

00305 
00306     def getpeercert(self, binary_form=False):
00307         """Returns a formatted version of the data in the
00308         certificate provided by the other end of the SSL channel.
00309         Return None if no certificate was provided, {} if a
00310         certificate was provided, but not validated."""
00311 
00312         self._checkClosed()
00313         return self._sslobj.peer_certificate(binary_form)

Here is the call graph for this function:

def socket.socket.makefile (   self,
  mode = "r",
  buffering = None,
  encoding = None,
  errors = None,
  newline = None 
) [inherited]
makefile(...) -> an I/O stream connected to the socket

The arguments are as for io.open() after the filename,
except the only mode characters supported are 'r', 'w' and 'b'.
The semantics are similar too.  (XXX refactor to share code?)

Definition at line 142 of file socket.py.

00142 
00143                  encoding=None, errors=None, newline=None):
00144         """makefile(...) -> an I/O stream connected to the socket
00145 
00146         The arguments are as for io.open() after the filename,
00147         except the only mode characters supported are 'r', 'w' and 'b'.
00148         The semantics are similar too.  (XXX refactor to share code?)
00149         """
00150         for c in mode:
00151             if c not in {"r", "w", "b"}:
00152                 raise ValueError("invalid mode %r (only r, w, b allowed)")
00153         writing = "w" in mode
00154         reading = "r" in mode or not writing
00155         assert reading or writing
00156         binary = "b" in mode
00157         rawmode = ""
00158         if reading:
00159             rawmode += "r"
00160         if writing:
00161             rawmode += "w"
00162         raw = SocketIO(self, rawmode)
00163         self._io_refs += 1
00164         if buffering is None:
00165             buffering = -1
00166         if buffering < 0:
00167             buffering = io.DEFAULT_BUFFER_SIZE
00168         if buffering == 0:
00169             if not binary:
00170                 raise ValueError("unbuffered streams must be binary")
00171             return raw
00172         if reading and writing:
00173             buffer = io.BufferedRWPair(raw, raw, buffering)
00174         elif reading:
00175             buffer = io.BufferedReader(raw, buffering)
00176         else:
00177             assert writing
00178             buffer = io.BufferedWriter(raw, buffering)
00179         if binary:
00180             return buffer
00181         text = io.TextIOWrapper(buffer, encoding, errors, newline)
00182         text.mode = mode
00183         return text

Here is the caller graph for this function:

def ssl.SSLSocket.pending (   self)

Definition at line 411 of file ssl.py.

00411 
00412     def pending(self):
00413         self._checkClosed()
00414         if self._sslobj:
00415             return self._sslobj.pending()
00416         else:
00417             return 0

Here is the call graph for this function:

def ssl.SSLSocket.read (   self,
  len = 0,
  buffer = None 
)
Read up to LEN bytes and return them.
Return zero-length string on EOF.

Definition at line 278 of file ssl.py.

00278 
00279     def read(self, len=0, buffer=None):
00280         """Read up to LEN bytes and return them.
00281         Return zero-length string on EOF."""
00282 
00283         self._checkClosed()
00284         try:
00285             if buffer is not None:
00286                 v = self._sslobj.read(len, buffer)
00287             else:
00288                 v = self._sslobj.read(len or 1024)
00289             return v
00290         except SSLError as x:
00291             if x.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:
00292                 if buffer is not None:
00293                     return 0
00294                 else:
00295                     return b''
00296             else:
00297                 raise

Here is the call graph for this function:

Here is the caller graph for this function:

def ssl.SSLSocket.recv (   self,
  buflen = 1024,
  flags = 0 
)

Definition at line 369 of file ssl.py.

00369 
00370     def recv(self, buflen=1024, flags=0):
00371         self._checkClosed()
00372         if self._sslobj:
00373             if flags != 0:
00374                 raise ValueError(
00375                     "non-zero flags not allowed in calls to recv() on %s" %
00376                     self.__class__)
00377             return self.read(buflen)
00378         else:
00379             return socket.recv(self, buflen, flags)

Here is the call graph for this function:

Here is the caller graph for this function:

def ssl.SSLSocket.recv_into (   self,
  buffer,
  nbytes = None,
  flags = 0 
)

Definition at line 380 of file ssl.py.

00380 
00381     def recv_into(self, buffer, nbytes=None, flags=0):
00382         self._checkClosed()
00383         if buffer and (nbytes is None):
00384             nbytes = len(buffer)
00385         elif nbytes is None:
00386             nbytes = 1024
00387         if self._sslobj:
00388             if flags != 0:
00389                 raise ValueError(
00390                   "non-zero flags not allowed in calls to recv_into() on %s" %
00391                   self.__class__)
00392             return self.read(nbytes, buffer)
00393         else:
00394             return socket.recv_into(self, buffer, nbytes, flags)

Here is the call graph for this function:

def ssl.SSLSocket.recvfrom (   self,
  buflen = 1024,
  flags = 0 
)

Definition at line 395 of file ssl.py.

00395 
00396     def recvfrom(self, buflen=1024, flags=0):
00397         self._checkClosed()
00398         if self._sslobj:
00399             raise ValueError("recvfrom not allowed on instances of %s" %
00400                              self.__class__)
00401         else:
00402             return socket.recvfrom(self, buflen, flags)

Here is the call graph for this function:

def ssl.SSLSocket.recvfrom_into (   self,
  buffer,
  nbytes = None,
  flags = 0 
)

Definition at line 403 of file ssl.py.

00403 
00404     def recvfrom_into(self, buffer, nbytes=None, flags=0):
00405         self._checkClosed()
00406         if self._sslobj:
00407             raise ValueError("recvfrom_into not allowed on instances of %s" %
00408                              self.__class__)
00409         else:
00410             return socket.recvfrom_into(self, buffer, nbytes, flags)

Here is the call graph for this function:

def ssl.SSLSocket.send (   self,
  data,
  flags = 0 
)

Definition at line 321 of file ssl.py.

00321 
00322     def send(self, data, flags=0):
00323         self._checkClosed()
00324         if self._sslobj:
00325             if flags != 0:
00326                 raise ValueError(
00327                     "non-zero flags not allowed in calls to send() on %s" %
00328                     self.__class__)
00329             while True:
00330                 try:
00331                     v = self._sslobj.write(data)
00332                 except SSLError as x:
00333                     if x.args[0] == SSL_ERROR_WANT_READ:
00334                         return 0
00335                     elif x.args[0] == SSL_ERROR_WANT_WRITE:
00336                         return 0
00337                     else:
00338                         raise
00339                 else:
00340                     return v
00341         else:
00342             return socket.send(self, data, flags)

Here is the call graph for this function:

Here is the caller graph for this function:

def ssl.SSLSocket.sendall (   self,
  data,
  flags = 0 
)

Definition at line 353 of file ssl.py.

00353 
00354     def sendall(self, data, flags=0):
00355         self._checkClosed()
00356         if self._sslobj:
00357             if flags != 0:
00358                 raise ValueError(
00359                     "non-zero flags not allowed in calls to sendall() on %s" %
00360                     self.__class__)
00361             amount = len(data)
00362             count = 0
00363             while (count < amount):
00364                 v = self.send(data[count:])
00365                 count += v
00366             return amount
00367         else:
00368             return socket.sendall(self, data, flags)

Here is the call graph for this function:

def ssl.SSLSocket.sendto (   self,
  data,
  flags_or_addr,
  addr = None 
)

Definition at line 343 of file ssl.py.

00343 
00344     def sendto(self, data, flags_or_addr, addr=None):
00345         self._checkClosed()
00346         if self._sslobj:
00347             raise ValueError("sendto not allowed on instances of %s" %
00348                              self.__class__)
00349         elif addr is None:
00350             return socket.sendto(self, data, flags_or_addr)
00351         else:
00352             return socket.sendto(self, data, flags_or_addr, addr)

Here is the call graph for this function:

def ssl.SSLSocket.shutdown (   self,
  how 
)

Definition at line 418 of file ssl.py.

00418 
00419     def shutdown(self, how):
00420         self._checkClosed()
00421         self._sslobj = None
00422         socket.shutdown(self, how)

Here is the call graph for this function:

def ssl.SSLSocket.unwrap (   self)

Definition at line 423 of file ssl.py.

00423 
00424     def unwrap(self):
00425         if self._sslobj:
00426             s = self._sslobj.shutdown()
00427             self._sslobj = None
00428             return s
00429         else:
00430             raise ValueError("No SSL wrapper around " + str(self))

def ssl.SSLSocket.write (   self,
  data 
)
Write DATA to the underlying SSL channel.  Returns
number of bytes of DATA actually transmitted.

Definition at line 298 of file ssl.py.

00298 
00299     def write(self, data):
00300         """Write DATA to the underlying SSL channel.  Returns
00301         number of bytes of DATA actually transmitted."""
00302 
00303         self._checkClosed()
00304         return self._sslobj.write(data)

Here is the call graph for this function:


Member Data Documentation

Reimplemented from socket.socket.

Definition at line 251 of file ssl.py.

Definition at line 253 of file ssl.py.

Definition at line 252 of file ssl.py.

Definition at line 220 of file ssl.py.

Definition at line 218 of file ssl.py.

Definition at line 217 of file ssl.py.

Definition at line 221 of file ssl.py.

Definition at line 199 of file ssl.py.

Definition at line 227 of file ssl.py.

Definition at line 216 of file ssl.py.

Definition at line 226 of file ssl.py.

Definition at line 225 of file ssl.py.

Definition at line 219 of file ssl.py.

Definition at line 228 of file ssl.py.


The documentation for this class was generated from the following file: