Helpers API

All public names from submodules client, multipart, protocol and utils are exported into aiohttp namespace.

WebSocket utilities

class aiohttp.WSCloseCode[source]

An IntEnum for 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_data or message_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 IntEnum for describing WSMessage type.

CONTINUATION

A mark for continuation frame, user will never get the message with this type.

TEXT

Text message, the value has str type.

BINARY

Binary message, the value has bytes type.

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.

type

Message type, WSMsgType instance.

data

Message payload.

  1. str for WSMsgType.TEXT messages.
  2. bytes for WSMsgType.BINARY messages.
  3. WSCloseCode for WSMsgType.CLOSE messages.
  4. bytes for WSMsgType.PING messages.
  5. bytes for WSMsgType.PONG messages.
extra

Additional info, str.

Makes sense only for WSMsgType.CLOSE messages, contains optional message description.

json(*, loads=json.loads)[source]

Returns parsed JSON data.

New in version 0.22.

Parameters:loads – optional JSON decoder function.
tp

Deprecated alias for type.

Deprecated since version 1.0.

aiohttp.helpers module

Various helper functions

aiohttp.helpers.create_future(loop)[source]
aiohttp.helpers.parse_mimetype(mimetype)[source]

Parses a MIME type into its components.

Parameters:mimetype (str) – MIME type
Returns:4 element tuple for MIME type, subtype, suffix and parameters
Return type:tuple

Example:

>>> parse_mimetype('text/html; charset=utf-8')
('text', 'html', '', {'charset': 'utf-8'})
aiohttp.helpers.Timeout

alias of timeout

aiohttp.helpers.ensure_future(coro_or_future, *, loop=None)[source]

Wrap a coroutine or an awaitable in a future.

If the argument is a Future, it is returned directly.

coroutine aiohttp.helpers.noop(*args, **kwargs)[source]
class aiohttp.helpers.DummyCookieJar(*, loop=None)[source]

Bases: aiohttp.abc.AbstractCookieJar

Implements a dummy cookie storage.

It can be used with the ClientSession when no cookie processing is needed.

clear()[source]
filter_cookies(request_url)[source]
update_cookies(cookies, response_url=None)[source]

aiohttp.multipart module

class aiohttp.multipart.MultipartReader(headers, content)[source]

Bases: object

Multipart body reader.

at_eof()[source]

Returns True if the final boundary was reached or False otherwise.

Return type:bool
coroutine fetch_next_part()[source]

Returns the next body part reader.

classmethod from_response(response)[source]

Constructs reader instance from HTTP response.

Parameters:responseClientResponse instance
multipart_reader_cls = None

Multipart reader class, used to handle multipart/* body parts. None points to type(self)

coroutine next()[source]

Emits the next multipart body part.

part_reader_cls

Body part reader class for non multipart/* content types.

alias of BodyPartReader

coroutine release()[source]

Reads all the body parts to the void till the final boundary.

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.Payload

Multipart body writer.

append(obj, headers=None)[source]
append_form(obj, headers=None)[source]

Helper to append form urlencoded part.

append_json(obj, headers=None)[source]

Helper to append JSON part.

append_payload(payload)[source]

Adds a new body part to multipart writer.

boundary
size

Size of the payload.

coroutine write(writer)[source]

Write body.

class aiohttp.multipart.BodyPartReader(boundary, headers, content)[source]

Bases: object

Multipart reader for single body part.

at_eof()[source]

Returns True if the boundary was reached or False otherwise.

Return type:bool
chunk_size = 8192
decode(data)[source]

Decodes data according the specified Content-Encoding or Content-Transfer-Encoding headers value.

Supports gzip, deflate and identity encodings for Content-Encoding header.

Supports base64, quoted-printable, binary encodings 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 None if 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
get_charset(default=None)[source]

Returns charset parameter from Content-Type header or default.

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 None if missed or header is malformed.

coroutine next()[source]
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
coroutine read_chunk(size=8192)[source]

Reads body part content chunk of the specified size.

Parameters:size (int) – chunk size
Return type:bytearray
coroutine readline()[source]

Reads body part by line by line.

Return type:bytearray
coroutine release()[source]

Like read(), but reads all the data to the void.

Return type:None
coroutine text(*, encoding=None)[source]

Like read(), but assumes that body part contains text data.

Parameters:encoding (str) – Custom text encoding. Overrides specified in charset param of Content-Type header
Return type:str
exception aiohttp.multipart.BadContentDispositionHeader[source]

Bases: RuntimeWarning

exception aiohttp.multipart.BadContentDispositionParam[source]

Bases: RuntimeWarning

aiohttp.multipart.parse_content_disposition(header)[source]
aiohttp.multipart.content_disposition_filename(params, name=’filename’)[source]

aiohttp.signals module

class aiohttp.signals.BaseSignal[source]

Bases: aiohttp._frozenlist.FrozenList

class aiohttp.signals.DebugSignal[source]

Bases: aiohttp.signals.BaseSignal

coroutine send(ordinal, name, *args, **kwargs)[source]
class aiohttp.signals.FuncSignal[source]

Bases: aiohttp.signals.BaseSignal

Callback-based signal implementation.

To connect a callback to a signal, use any list method.

Signals are fired using the send() method, which takes named arguments.

send(*args, **kwargs)[source]

Sends data to all registered receivers.

class aiohttp.signals.PostSignal[source]

Bases: aiohttp.signals.DebugSignal

class aiohttp.signals.PreSignal[source]

Bases: aiohttp.signals.DebugSignal

ordinal()[source]
class aiohttp.signals.Signal(app)[source]

Bases: aiohttp.signals.BaseSignal

Coroutine-based signal implementation.

To connect a callback to a signal, use any list method.

Signals are fired using the send() coroutine, which takes named arguments.

coroutine send(*args, **kwargs)[source]

Sends data to all registered receivers.

blog comments powered by Disqus