adafruit_requests

A requests-like library for web interfacing

  • Author(s): ladyada, Paul Sokolovsky, Scott Shawcroft

Implementation Notes

Adapted from https://github.com/micropython/micropython-lib/tree/master/urequests

micropython-lib consists of multiple modules from different sources and authors. Each module comes under its own licensing terms. Short name of a license can be found in a file within a module directory (usually metadata.txt or setup.py). Complete text of each license used is provided at https://github.com/micropython/micropython-lib/blob/master/LICENSE

author=’Paul Sokolovsky’ license=’MIT’

Software and Dependencies:

class adafruit_requests.CircuitPythonSocketType(*args, **kwargs)

Describes the structure every modern CircuitPython socket type must have.

class adafruit_requests.CommonCircuitPythonSocketType(*args, **kwargs)

Describes the common structure every CircuitPython socket type must have.

connect(address: Tuple[str, int], conntype: int | None = Ellipsis) None

Connect to a remote socket at the provided (host, port) address. The conntype kwarg optionally may indicate SSL or not, depending on the underlying interface.

class adafruit_requests.CommonSocketType(*args, **kwargs)

Describes the common structure every socket type must have.

close() None

Close the socket.

send(data: bytes, flags: int = Ellipsis) None

Send data to the socket. The meaning of the optional flags kwarg is implementation-specific.

settimeout(value: float | None) None

Set a timeout on blocking socket operations.

class adafruit_requests.InterfaceType(*args, **kwargs)

Describes the structure every interface type must have.

property TLS_MODE: int

Constant representing that a socket’s connection mode is TLS.

exception adafruit_requests.OutOfRetries

Raised when requests has retried to make a request unsuccessfully.

class adafruit_requests.Response(sock: CircuitPythonSocketType | StandardPythonSocketType, session: Session | None = None)

The response from a request, contains all the headers/content

close() None

Drain the remaining ESP socket buffers. We assume we already got what we wanted.

property content: bytes

The HTTP content direct from the socket, as bytes

property headers: Dict[str, str]

The response headers. Does not include headers from the trailer until the content has been read.

iter_content(chunk_size: int = 1, decode_unicode: bool = False) bytes

An iterator that will stream data by only reading ‘chunk_size’ bytes and yielding them, when we can’t buffer the whole datastream

json() Any

The HTTP content, parsed into a json dictionary

reason: bytearray

The status reason returned by the server

status_code: int

The status code returned by the server

property text: str

The HTTP content, encoded into a string according to the HTTP header encoding

class adafruit_requests.Session(socket_pool: module, ssl_context: SSLContext | _FakeSSLContext | None = None)

HTTP session that shares sockets and ssl context.

delete(url: str, **kw) Response

Send HTTP DELETE request

get(url: str, **kw) Response

Send HTTP GET request

head(url: str, **kw) Response

Send HTTP HEAD request

patch(url: str, **kw) Response

Send HTTP PATCH request

post(url: str, **kw) Response

Send HTTP POST request

put(url: str, **kw) Response

Send HTTP PUT request

request(method: str, url: str, data: Any | None = None, json: Any | None = None, headers: Dict[str, str] | None = None, stream: bool = False, timeout: float = 60, allow_redirects: bool = True) Response

Perform an HTTP request to the given url which we will parse to determine whether to use SSL (’https://’) or not. We can also send some provided ‘data’ or a json dictionary which we will stringify. ‘headers’ is optional HTTP headers sent along. ‘stream’ will determine if we buffer everything, or whether to only read only when requested

class adafruit_requests.StandardPythonSocketType(*args, **kwargs)

Describes the structure every standard Python socket type must have.

connect(address: Tuple[Any, ...] | str | bytes) None

Connect to a remote socket at the provided address.

class adafruit_requests.SupportsRecvInto(*args, **kwargs)

Describes a type that possesses a socket recv_into() method.

recv_into(buffer: bytearray, nbytes: int = Ellipsis, flags: int = Ellipsis) int

Receive up to nbytes bytes from the socket, storing the data into the provided buffer. If nbytes is not specified (or 0), receive up to the size available in the given buffer. The meaning of the optional flags kwarg is implementation-specific. Returns the number of bytes received.

class adafruit_requests.SupportsRecvWithFlags(*args, **kwargs)

Describes a type that posseses a socket recv() method supporting the flags kwarg.

recv(bufsize: int = Ellipsis, flags: int = Ellipsis) bytes

Receive data from the socket. The return value is a bytes object representing the data received. The maximum amount of data to be received at once is specified by bufsize. The meaning of the optional flags kwarg is implementation-specific.

adafruit_requests.delete(url: str, **kw)

Send HTTP DELETE request

adafruit_requests.get(url: str, **kw)

Send HTTP GET request

adafruit_requests.head(url: str, **kw)

Send HTTP HEAD request

adafruit_requests.patch(url: str, **kw)

Send HTTP PATCH request

adafruit_requests.post(url: str, **kw)

Send HTTP POST request

adafruit_requests.put(url: str, **kw)

Send HTTP PUT request

adafruit_requests.request(method: str, url: str, data: Any | None = None, json: Any | None = None, headers: Dict[str, str] | None = None, stream: bool = False, timeout: float = 1) None

Send HTTP request

adafruit_requests.set_socket(sock: module, iface: InterfaceType | None = None) None

Legacy API for setting the socket and network interface. Use a Session instead.