Helpers API¶
All public names from submodules
errors
, multipart
, parsers
, protocol
, utils
,
websocket
and wsgi
are exported into aiohttp
namespace.
aiohttp.errors module¶
http related errors.
-
exception
aiohttp.errors.
ClientDisconnectedError
[source]¶ Bases:
aiohttp.errors.DisconnectedError
Client disconnected.
-
exception
aiohttp.errors.
ServerDisconnectedError
[source]¶ Bases:
aiohttp.errors.DisconnectedError
Server disconnected.
-
exception
aiohttp.errors.
HttpProcessingError
(*, code=None, message='', headers=None)[source]¶ Bases:
Exception
Http error.
Shortcut for raising http errors with custom code, message and headers.
Parameters: -
code
= 0¶
-
headers
= None¶
-
message
= ''¶
-
-
exception
aiohttp.errors.
BadHttpMessage
(message, *, headers=None)[source]¶ Bases:
aiohttp.errors.HttpProcessingError
-
code
= 400¶
-
message
= 'Bad Request'¶
-
-
exception
aiohttp.errors.
HttpMethodNotAllowed
(*, code=None, message='', headers=None)[source]¶ Bases:
aiohttp.errors.HttpProcessingError
-
code
= 405¶
-
message
= 'Method Not Allowed'¶
-
-
exception
aiohttp.errors.
HttpBadRequest
(message, *, headers=None)[source]¶ Bases:
aiohttp.errors.BadHttpMessage
-
code
= 400¶
-
message
= 'Bad Request'¶
-
-
exception
aiohttp.errors.
HttpProxyError
(*, code=None, message='', headers=None)[source]¶ Bases:
aiohttp.errors.HttpProcessingError
Http proxy error.
Raised in
aiohttp.connector.ProxyConnector
if proxy responds with status other than200 OK
onCONNECT
request.
-
exception
aiohttp.errors.
ClientError
[source]¶ Bases:
Exception
Base class for client connection errors.
-
exception
aiohttp.errors.
ClientHttpProcessingError
[source]¶ Bases:
aiohttp.errors.ClientError
Base class for client http processing errors.
-
exception
aiohttp.errors.
ClientConnectionError
[source]¶ Bases:
aiohttp.errors.ClientError
Base class for client socket errors.
-
exception
aiohttp.errors.
ClientOSError
[source]¶ Bases:
aiohttp.errors.ClientConnectionError
,OSError
OSError error.
-
exception
aiohttp.errors.
ClientTimeoutError
[source]¶ Bases:
aiohttp.errors.ClientConnectionError
,concurrent.futures._base.TimeoutError
Client connection timeout error.
-
exception
aiohttp.errors.
ProxyConnectionError
[source]¶ Bases:
aiohttp.errors.ClientConnectionError
Proxy connection error.
Raised in
aiohttp.connector.ProxyConnector
if connection to proxy can not be established.
-
exception
aiohttp.errors.
ClientRequestError
[source]¶ Bases:
aiohttp.errors.ClientHttpProcessingError
Connection error during sending request.
-
exception
aiohttp.errors.
ClientResponseError
[source]¶ Bases:
aiohttp.errors.ClientHttpProcessingError
Connection error during reading response.
-
exception
aiohttp.errors.
FingerprintMismatch
(expected, got, host, port)[source]¶ Bases:
aiohttp.errors.ClientConnectionError
SSL certificate does not match expected fingerprint.
-
exception
aiohttp.errors.
WSServerHandshakeError
(*, code=None, message='', headers=None)[source]¶ Bases:
aiohttp.errors.HttpProcessingError
websocket server handshake error.
-
exception
aiohttp.errors.
WSClientDisconnectedError
[source]¶ Bases:
aiohttp.errors.ClientDisconnectedError
Deprecated.
aiohttp.helpers module¶
Various helper functions
-
class
aiohttp.helpers.
FormData
(fields=())[source]¶ Bases:
object
Helper class for multipart/form-data and application/x-www-form-urlencoded body generation.
-
add_field
(name, value, *, content_type=None, filename=None, content_transfer_encoding=None)[source]¶
-
content_type
¶
-
is_multipart
¶
-
-
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'})
-
class
aiohttp.helpers.
Timeout
(timeout, *, loop=None)[source]¶ Bases:
object
Timeout context manager.
Useful in cases when you want to apply timeout logic around block of code or in cases when asyncio.wait_for is not suitable. For example:
>>> with aiohttp.Timeout(0.001): ... async with aiohttp.get('https://github.com') as r: ... await r.text()
Parameters: - timeout – timeout value in seconds
- loop – asyncio compatible event loop
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 orFalse
otherwise.Return type: bool
-
classmethod
from_response
(response)[source]¶ Constructs reader instance from HTTP response.
Parameters: response – ClientResponse
instance
-
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:
object
Multipart body writer.
-
boundary
¶
-
part_writer_cls
¶ Body part reader class for non multipart/* content types.
alias of
BodyPartWriter
-
-
class
aiohttp.multipart.
BodyPartReader
(boundary, headers, content)[source]¶ Bases:
object
Multipart 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
,deflate
andidentity
encodings for Content-Encoding header.Supports
base64
,quoted-printable
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.
-
form
(*, encoding=None)[source]¶ Lke
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
-
json
(*, encoding=None)[source]¶ Lke
read()
, but assumes that body parts contains JSON data.Parameters: encoding (str) – Custom JSON encoding. Overrides specified in charset param of Content-Type header
-
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
-
-
class
aiohttp.multipart.
BodyPartWriter
(obj, headers=None, *, chunk_size=8192)[source]¶ Bases:
object
Multipart writer for single body part.
-
filename
¶ Returns filename specified in Content-Disposition header or
None
if missed.
-
-
exception
aiohttp.multipart.
BadContentDispositionHeader
[source]¶ Bases:
RuntimeWarning
-
exception
aiohttp.multipart.
BadContentDispositionParam
[source]¶ Bases:
RuntimeWarning
aiohttp.parsers module¶
Parser is a generator function (NOT coroutine).
Parser receives data with generator’s send() method and sends data to destination DataQueue. Parser receives ParserBuffer and DataQueue objects as a parameters of the parser call, all subsequent send() calls should send bytes objects. Parser sends parsed term to destination buffer with DataQueue.feed_data() method. DataQueue object should implement two methods. feed_data() - parser uses this method to send parsed protocol data. feed_eof() - parser uses this method for indication of end of parsing stream. To indicate end of incoming data stream EofStream exception should be sent into parser. Parser could throw exceptions.
There are three stages:
Data flow chain:
Application creates StreamParser object for storing incoming data.
StreamParser creates ParserBuffer as internal data buffer.
Application create parser and set it into stream buffer:
parser = HttpRequestParser() data_queue = stream.set_parser(parser)
At this stage StreamParser creates DataQueue object and passes it and internal buffer into parser as an arguments.
- def set_parser(self, parser):
output = DataQueue() self.p = parser(output, self._input) return output
Application waits data on output.read()
- while True:
msg = yield from output.read() ...
Data flow:
- asyncio’s transport reads data from socket and sends data to protocol with data_received() call.
- Protocol sends data to StreamParser with feed_data() call.
- StreamParser sends data into parser with generator’s send() method.
- Parser processes incoming data and sends parsed data to DataQueue with feed_data()
- Application received parsed data from DataQueue.read()
Eof:
- StreamParser receives eof with feed_eof() call.
- StreamParser throws EofStream exception into parser.
- Then it unsets parser.
- _SocketSocketTransport ->
- -> “protocol” -> StreamParser -> “parser” -> DataQueue <- “application”
-
class
aiohttp.parsers.
StreamParser
(*, loop=None, buf=None, limit=65536, eof_exc_class=<class 'RuntimeError'>, **kwargs)[source]¶ Bases:
object
StreamParser manages incoming bytes stream and protocol parsers.
StreamParser uses ParserBuffer as internal buffer.
set_parser() sets current parser, it creates DataQueue object and sends ParserBuffer and DataQueue into parser generator.
unset_parser() sends EofStream into parser and then removes it.
-
output
¶
-
-
class
aiohttp.parsers.
StreamProtocol
(*, loop=None, disconnect_error=<class 'RuntimeError'>, **kwargs)[source]¶ Bases:
asyncio.streams.FlowControlMixin
,asyncio.protocols.Protocol
Helper class to adapt between Protocol and StreamReader.
-
class
aiohttp.parsers.
ParserBuffer
(*args)[source]¶ Bases:
object
ParserBuffer is NOT a bytearray extension anymore.
ParserBuffer provides helper methods for parsers.
aiohttp.signals module¶
-
class
aiohttp.signals.
DebugSignal
[source]¶ Bases:
aiohttp.signals.BaseSignal
-
send
(ordinal, name, *args, **kwargs)¶
-
-
class
aiohttp.signals.
PostSignal
[source]¶ Bases:
aiohttp.signals.DebugSignal
-
class
aiohttp.signals.
PreSignal
[source]¶ Bases:
aiohttp.signals.DebugSignal
-
ordinal
()¶
-
-
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.
aiohttp.streams module¶
-
class
aiohttp.streams.
StreamReader
(limit=65536, loop=None)[source]¶ Bases:
asyncio.streams.StreamReader
,aiohttp.streams.AsyncStreamReaderMixin
An enhancement of
asyncio.StreamReader
.Supports asynchronous iteration by line, chunk or as available:
async for line in reader: ... async for chunk in reader.iter_chunked(1024): ... async for slice in reader.iter_any(): ...
-
total_bytes
= 0¶
-
-
class
aiohttp.streams.
DataQueue
(*, loop=None)[source]¶ Bases:
object
DataQueue is a general-purpose blocking queue with one reader.
-
class
aiohttp.streams.
ChunksQueue
(*, loop=None)[source]¶ Bases:
aiohttp.streams.DataQueue
Like a
DataQueue
, but for binary chunked data transfer.-
readany
()¶
-
-
class
aiohttp.streams.
FlowControlStreamReader
(stream, limit=65536, *args, **kwargs)[source]¶ Bases:
aiohttp.streams.StreamReader
-
feed_data
(data, size=0)¶
-
read
(n=-1)¶
-
readany
()¶
-
readexactly
(n)¶
-
readline
()¶
-
-
class
aiohttp.streams.
FlowControlDataQueue
(stream, *, limit=65536, loop=None)[source]¶ Bases:
aiohttp.streams.DataQueue
FlowControlDataQueue resumes and pauses an underlying stream.
It is a destination for parsed data.
-
class
aiohttp.streams.
FlowControlChunksQueue
(stream, *, limit=65536, loop=None)[source]¶ Bases:
aiohttp.streams.FlowControlDataQueue
-
read
()¶
-
readany
()¶
-
aiohttp.websocket module¶
WebSocket protocol versions 13 and 8.
-
class
aiohttp.websocket.
WebSocketWriter
(writer, *, use_mask=False, random=<random.Random object at 0x347fdc8>)[source]¶ Bases:
object
-
close
(code=1000, message=b'')¶ Close the websocket, sending the specified code and message.
-
ping
(message=b'')¶ Send ping message.
-
pong
(message=b'')¶ Send pong message.
-
send
(message, binary=False)¶ Send a frame over the websocket with message as its payload.
-
-
aiohttp.websocket.
do_handshake
(method, headers, transport, protocols=())[source]¶ Prepare WebSocket handshake.
It return http response code, response headers, websocket parser, websocket writer. It does not perform any IO.
protocols is a sequence of known protocols. On successful handshake, the returned response headers contain the first protocol in this list which the server also knows.
aiohttp.wsgi module¶
wsgi server.
- TODO:
- proxy protocol
- x-forward security
- wsgi file support (os.sendfile)
-
class
aiohttp.wsgi.
WSGIServerHttpProtocol
(app, readpayload=False, is_ssl=False, *args, **kw)[source]¶ Bases:
aiohttp.server.ServerHttpProtocol
HTTP Server that implements the Python WSGI protocol.
It uses ‘wsgi.async’ of ‘True’. ‘wsgi.input’ can behave differently depends on ‘readpayload’ constructor parameter. If readpayload is set to True, wsgi server reads all incoming data into BytesIO object and sends it as ‘wsgi.input’ environ var. If readpayload is set to false ‘wsgi.input’ is a StreamReader and application should read incoming data with “yield from environ[‘wsgi.input’].read()”. It defaults to False.
-
SCRIPT_NAME
= ''¶
-