ssl — Adafruit CircuitPython 1 documentation (original) (raw)

Adafruit CircuitPython

The ssl module provides SSL contexts to wrap sockets in.

This module implements a subset of the corresponding CPython module, as described below. For more information, refer to the original CPython documentation: cpython:ssl.

Available on these boards

ssl.create_default_context() → SSLContext

Return the default SSLContext.

class ssl.SSLContext

Settings related to SSL that can be applied to a socket by wrapping it. This is useful to provide SSL certificates to specific connections rather than all of them.

load_cert_chain(certfile: str, keyfile: str) → None

Load a private key and the corresponding certificate.

The certfile string must be the path to a single file in PEM format containing the certificate as well as any number of CA certificates needed to establish the certificate’s authenticity. The keyfile string must point to a file containing the private key.

load_verify_locations(cafile: str | None = None, capath: str | None = None, cadata: str | None = None) → None

Load a set of certification authority (CA) certificates used to validate other peers’ certificates.

Parameters:

set_default_verify_paths() → None

Load a set of default certification authority (CA) certificates.

check_hostname_: bool_

Whether to match the peer certificate’s hostname.

wrap_socket(sock: socketpool.Socket, *, server_side: bool = False, server_hostname: str | None = None) → SSLSocket

Wraps the socket into a socket-compatible class that handles SSL negotiation. The socket must be of type SOCK_STREAM.

class ssl.SSLSocket

Implements TLS security on a subset of socketpool.Socket functions. Cannot be created directly. Instead, call wrap_socket on an existing socket object.

Provides a subset of CPython’s ssl.SSLSocket API. It only implements the versions of recv that do not allocate bytes objects.

__hash__() → int

Returns a hash for the Socket.

__enter__() → SSLSocket

No-op used by Context Managers.

__exit__() → None

Automatically closes the Socket when exiting a context. SeeLifetime and ContextManagers for more info.

accept() → Tuple[SSLSocket, Tuple[str, int]]

Accept a connection on a listening socket of type SOCK_STREAM, creating a new socket of type SOCK_STREAM. Returns a tuple of (new_socket, remote_address)

bind(address: Tuple[str, int]) → None

Bind a socket to an address

Parameters:

address (~tuple) – tuple of (remote_address, remote_port)

close() → None

Closes this Socket

connect(address: Tuple[str, int]) → None

Connect a socket to a remote address

Parameters:

address (~tuple) – tuple of (remote_address, remote_port)

listen(backlog: int) → None

Set socket to listen for incoming connections

Parameters:

backlog (~int) – length of backlog queue for waiting connetions

recv_into(buffer: circuitpython_typing.WriteableBuffer, bufsize: int) → int

Reads some bytes from the connected remote address, writing into the provided buffer. If bufsize <= len(buffer) is given, a maximum of bufsize bytes will be read into the buffer. If no valid value is given for bufsize, the default is the length of the given buffer.

Suits sockets of type SOCK_STREAM Returns an int of number of bytes read.

Parameters:

send(bytes: circuitpython_typing.ReadableBuffer) → int

Send some bytes to the connected remote address. Suits sockets of type SOCK_STREAM

Parameters:

bytes (~bytes) – some bytes to send

settimeout(value: int) → None

Set the timeout value for this socket.

Parameters:

value (~int) – timeout in seconds. 0 means non-blocking. None means block indefinitely.

setblocking(flag: bool) → int | None

Set the blocking behaviour of this socket.

Parameters:

flag (~bool) – False means non-blocking, True means block indefinitely.