On this page
ssl.SSLSocket
Prev: ssl.SSLSession | Up: ssl | Next: ssl.SSLSyscallError
Rel: ssl.AlertDescription, ssl.cert_time_to_seconds, ssl.CertificateError, ssl.create_connection
Ref: docs.python.org — ssl.SSLSocket
Tags: #public #python3 #stdlib #ssl
Import
import ssl
from ssl import SSLSocket
Signature & Overview
ssl.SSLSocket(*args, **kwargs)
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.
Methods & Attributes
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.SSLSocket.bind(self, object, /)— Bind the socket to a local address. For IP sockets, the address is a pair (host, port); the host must refer to the local host. For raw packet sockets the address is a tuple (ifname, proto [,pkttype [,hatype [,addr]]])SSLSocket.cipher(self)— Return the currently selected cipher as a 3-tuple(name, ssl_version, secret_bits).SSLSocket.close(self)—SSLSocket.closemethod/property.SSLSocket.compression(self)— Return the current compression algorithm in use, orNoneif compression was not negotiated or not supported by one of the peers.SSLSocket.connect(self, addr)— Connects to remote ADDR, and then wraps the connection in an SSL channel.SSLSocket.connect_ex(self, addr)— Connects to remote ADDR, and then wraps the connection in an SSL channel.SSLSocket.context(...)— The SSLContext that is currently in use.SSLSocket.detach(self)— Close the socket object without closing the underlying file descriptor.SSLSocket.do_handshake(self, block=False)— Start the SSL/TLS handshake.SSLSocket.dup(self)—SSLSocket.dupmethod/property.SSLSocket.family(...)— Read-only access to the address family for this socket.SSLSocket.fileno(self, /)— Return the integer file descriptor of the socket.SSLSocket.get_channel_binding(self, cb_type='tls-unique')— Get channel binding data for current connection. Raise ValueError if the requestedcb_typeis not supported. Return bytes of the data or None if the data is not available (e.g. before the handshake).SSLSocket.get_inheritable(self)— Get the inheritable flag of the socketSSLSocket.get_unverified_chain(self)— Returns raw certificate chain provided by the other end of the SSL channel as a list of DER-encoded bytes.SSLSocket.get_verified_chain(self)— Returns verified certificate chain provided by the other end of the SSL channel as a list of DER-encoded bytes.SSLSocket.getblocking(self, /)— Returns True if socket is in blocking mode, or False if it is in non-blocking mode.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.SSLSocket.getpeername(self, /)— Return the address of the remote endpoint. For IP sockets, the address info is a pair (hostaddr, port).SSLSocket.getsockname(self, /)— Return the address of the local endpoint. The format depends on the address family. For IPv4 sockets, the address info is a pair (hostaddr, port). For IPv6 sockets, the address info is a 4-tupleSSLSocket.getsockopt(...)— Get a socket option. See the Unix manual for level and option.SSLSocket.gettimeout(self, /)— Returns the timeout in seconds (float) associated with socket operations. A timeout of None indicates that timeouts on socket operations are disabled.SSLSocket.listen(...)— Enable a server to accept connections. If backlog is specified, it must be at least 0 (if it is lower, it is set to 0); it specifies the number of unaccepted connections that the system will allow before refusing newSSLSocket.makefile(self, mode='r', buffering=None, *, encoding=None, errors=None, newline=None)— The arguments are as for io.open() after the filename, except the only supported mode values are 'r' (default), 'w', 'b', or a combination of those.SSLSocket.pending(self)— Return the number of bytes that can be read immediately.SSLSocket.read(self, len=1024, buffer=None)— Read up to LEN bytes and return them.SSLSocket.recv(self, buflen=1024, flags=0)—SSLSocket.recvmethod/property.SSLSocket.recv_into(self, buffer, nbytes=None, flags=0)—SSLSocket.recv_intomethod/property.SSLSocket.recvfrom(self, buflen=1024, flags=0)—SSLSocket.recvfrommethod/property.SSLSocket.recvfrom_into(self, buffer, nbytes=None, flags=0)—SSLSocket.recvfrom_intomethod/property.SSLSocket.recvmsg(self, *args, **kwargs)—SSLSocket.recvmsgmethod/property.SSLSocket.recvmsg_into(self, *args, **kwargs)—SSLSocket.recvmsg_intomethod/property.SSLSocket.selected_alpn_protocol(self)— Return the currently selected ALPN protocol as a string, orNoneif a next protocol was not negotiated or if ALPN is not supported by one of the peers.SSLSocket.selected_npn_protocol(self)— Return the currently selected NPN protocol as a string, orNoneif a next protocol was not negotiated or if NPN is not supported by one of the peers.SSLSocket.send(self, data, flags=0)—SSLSocket.sendmethod/property.SSLSocket.sendall(self, data, flags=0)—SSLSocket.sendallmethod/property.SSLSocket.sendfile(self, file, offset=0, count=None)— Send a file, possibly by using os.sendfile() if this is a clear-text socket. Return the total number of bytes sent.SSLSocket.sendmsg(self, *args, **kwargs)—SSLSocket.sendmsgmethod/property.SSLSocket.sendto(self, data, flags_or_addr, addr=None)—SSLSocket.sendtomethod/property.
Usage
import ssl
# Created by SSLContext.wrap_socket(raw_sock, server_hostname="...")
assert issubclass(ssl.SSLSocket, ssl.socket)
Gotchas
- Always pass
server_hostname=on client sockets: Required for both SNI (Server Name Indication) and hostname certificate verification.
REPL Inspect
from ssl import SSLSocket
help(SSLSocket)
Linked from
Related notes
- tuts/python3/stdlib_module/ssl/SSLSession Linked here; links to this page; shared tags: #ssl, #stdlib
- tuts/python3/stdlib_module/ssl/SSLSyscallError Linked here; links to this page; shared tags: #ssl, #stdlib
- tuts/python3/stdlib_module/ssl Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ssl/AlertDescription Linked here; shared tags: #ssl, #stdlib
- tuts/python3/stdlib_module/ssl/cert_time_to_seconds Linked here; shared tags: #ssl, #stdlib
- tuts/python3/stdlib_module/ssl/CertificateError Linked here; shared tags: #ssl, #stdlib
- tuts/python3/stdlib_module/ssl/create_connection Linked here; shared tags: #ssl, #stdlib
- tuts/python3/stdlib_module/ssl/create_default_context shared tags: #ssl, #stdlib
- tuts/python3/stdlib_module/ssl/DefaultVerifyPaths shared tags: #ssl, #stdlib
- tuts/python3/stdlib_module/ssl/DER_cert_to_PEM_cert shared tags: #ssl, #stdlib