Helpers API¶
All public names from submodules client, multipart,
protocol and utils are exported into
aiohttp namespace.
WebSocket utilities¶
-
class
aiohttp.WSCloseCode[source]¶ An
IntEnumfor keeping close message code.-
OK¶ A normal closure, meaning that the purpose for which the connection was established has been fulfilled.
-
GOING_AWAY¶ An endpoint is “going away”, such as a server going down or a browser having navigated away from a page.
-
PROTOCOL_ERROR¶ An endpoint is terminating the connection due to a protocol error.
-
UNSUPPORTED_DATA¶ An endpoint is terminating the connection because it has received a type of data it cannot accept (e.g., an endpoint that understands only text data MAY send this if it receives a binary message).
-
INVALID_TEXT¶ An endpoint is terminating the connection because it has received data within a message that was not consistent with the type of the message (e.g., non-UTF-8 RFC 3629 data within a text message).
-
POLICY_VIOLATION¶ An endpoint is terminating the connection because it has received a message that violates its policy. This is a generic status code that can be returned when there is no other more suitable status code (e.g.,
unsupported_dataormessage_too_big) or if there is a need to hide specific details about the policy.
-
MESSAGE_TOO_BIG¶ An endpoint is terminating the connection because it has received a message that is too big for it to process.
-
MANDATORY_EXTENSION¶ An endpoint (client) is terminating the connection because it has expected the server to negotiate one or more extension, but the server did not return them in the response message of the WebSocket handshake. The list of extensions that are needed should appear in the /reason/ part of the Close frame. Note that this status code is not used by the server, because it can fail the WebSocket handshake instead.
-
INTERNAL_ERROR¶ A server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request.
-
SERVICE_RESTART¶ The service is restarted. a client may reconnect, and if it chooses to do, should reconnect using a randomized delay of 5-30s.
-
TRY_AGAIN_LATER¶ The service is experiencing overload. A client should only connect to a different IP (when there are multiple for the target) or reconnect to the same IP upon user action.
-
-
class
aiohttp.WSMsgType[source]¶ An
IntEnumfor describingWSMessagetype.-
CONTINUATION¶ A mark for continuation frame, user will never get the message with this type.
-
TEXT¶ Text message, the value has
strtype.
-
BINARY¶ Binary message, the value has
bytestype.
-
PING¶ Ping frame (sent by client peer).
-
PONG¶ Pong frame, answer on ping. Sent by server peer.
-
CLOSE¶ Close frame.
-
CLOSED FRAME Actually not frame but a flag indicating that websocket was closed.
-
ERROR¶ Actually not frame but a flag indicating that websocket was received an error.
-
-
class
aiohttp.WSMessage[source]¶ Websocket message, returned by
.receive()calls.-
data¶ Message payload.
strforWSMsgType.TEXTmessages.bytesforWSMsgType.BINARYmessages.WSCloseCodeforWSMsgType.CLOSEmessages.bytesforWSMsgType.PINGmessages.bytesforWSMsgType.PONGmessages.
-
extra¶ Additional info,
str.Makes sense only for
WSMsgType.CLOSEmessages, contains optional message description.
-
Signals¶
Signal is a list of registered asynchronous callbacks.
The signal’s life-cycle has two stages: after creation it’s content
could be filled by using standard list operations: sig.append()
etc.
After sig.freeze() call the signal is frozen: adding, removing
and dropping callbacks are forbidden.
The only available operation is calling previously registered
callbacks by await sig.send(data).
For concrete usage examples see signals in aiohttp.web chapter.
aiohttp.multipart module¶
-
class
aiohttp.multipart.MultipartReader(headers, content)[source]¶ Bases:
objectMultipart body reader.
-
at_eof()[source]¶ Returns
Trueif the final boundary was reached orFalseotherwise.Return type: bool
-
classmethod
from_response(response)[source]¶ Constructs reader instance from HTTP response.
Parameters: response – ClientResponseinstance
-
multipart_reader_cls= None¶ Multipart reader class, used to handle multipart/* body parts. None points to type(self)
-
part_reader_cls¶ Body part reader class for non multipart/* content types.
alias of
BodyPartReader
-
response_wrapper_cls¶ Response wrapper, used when multipart readers constructs from response.
alias of
MultipartResponseWrapper
-
-
class
aiohttp.multipart.MultipartWriter(subtype='mixed', boundary=None)[source]¶ Bases:
aiohttp.payload.PayloadMultipart body writer.
-
boundary¶
-
size¶ Size of the payload.
-
-
class
aiohttp.multipart.BodyPartReader(boundary, headers, content)[source]¶ Bases:
objectMultipart reader for single body part.
-
chunk_size= 8192¶
-
decode(data)[source]¶ Decodes data according the specified Content-Encoding or Content-Transfer-Encoding headers value.
Supports
gzip,deflateandidentityencodings for Content-Encoding header.Supports
base64,quoted-printable,binaryencodings for Content-Transfer-Encoding header.Parameters: data (bytearray) – Data to decode. Raises: RuntimeError- if encoding is unknown.Return type: bytes
-
filename¶ Returns filename specified in Content-Disposition header or
Noneif missed or header is malformed.
-
coroutine
form(*, encoding=None)[source]¶ Like
read(), but assumes that body parts contains form urlencoded data.Parameters: encoding (str) – Custom form encoding. Overrides specified in charset param of Content-Type header
-
coroutine
json(*, encoding=None)[source]¶ Like
read(), but assumes that body parts contains JSON data.Parameters: encoding (str) – Custom JSON encoding. Overrides specified in charset param of Content-Type header
-
name¶ Returns filename specified in Content-Disposition header or
Noneif missed or header is malformed.
-
coroutine
read(*, decode=False)[source]¶ Reads body part data.
Parameters: decode (bool) – Decodes data following by encoding method from Content-Encoding header. If it missed data remains untouched Return type: bytearray
-