Migration to 2.x¶
aiohttp does not support custom chunking sizes. It is up to the developer to decide how to chunk data streams. If chunking is enabled, aiohttp encodes the provided chunks in the “Transfer-encoding: chunked” format.
aiohttp does not enable chunked encoding automatically even if a transfer-encoding header is supplied: chunked has to be set explicitly. If chunked is set, then the Transfer-encoding and content-length headers are disallowed.
Compression has to be enabled explicitly with the compress parameter. If compression is enabled, adding a content-encoding header is not allowed. Compression also enables the chunked transfer-encoding. Compression can not be combined with a Content-Length header.
By default a connector object manages a total number of concurrent connections. This limit was a per host rule in version 1.x. In 2.x, the limit parameter defines how many concurrent connection connector can open and a new limit_per_host parameter defines the limit per host. By default there is no per-host limit.
BaseConnector.close is now a normal function as opposed to coroutine in version 1.x
BaseConnector.conn_timeout was moved to ClientSession
Internal implementation was significantly redesigned. It is not required to call release on the response object. When the client fully receives the payload, the underlying connection automatically returns back to pool. If the payload is not fully read, the connection is closed
Exception hierarchy has been significantly modified. aiohttp now defines only exceptions that covers connection handling and server response misbehaviors. For developer specific mistakes, aiohttp uses python standard exceptions like ValueError or TypeError.
Reading a response content may raise a ClientPayloadError exception. This exception indicates errors specific to the payload encoding. Such as invalid compressed data, malformed chunked-encoded chunks or not enough data that satisfy the content-length header.
All exceptions are moved from aiohttp.errors module to top level aiohttp module.
New hierarchy of exceptions:
ClientError - Base class for all client specific exceptions
ClientResponseError - exceptions that could happen after we get response from server
WSServerHandshakeError - web socket server response error
ClientHttpProxyError - proxy response
ClientConnectionError - exceptions related to low-level connection problems
ClientOSError - subset of connection errors that are initiated by an OSError exception
ClientConnectorError - connector related exceptions
ClientProxyConnectionError - proxy connection initialization error
ServerConnectionError - server connection related errors
ServerDisconnectedError - server disconnected
ServerTimeoutError - server operation timeout, (read timeout, etc)
ServerFingerprintMismatch - server fingerprint mismatch
ClientPayloadError - This exception can only be raised while reading the response payload if one of these errors occurs: invalid compression, malformed chunked encoding or not enough data that satisfy content-length header.
Client payload (form-data)¶
To unify form-data/payload handling a new Payload system was introduced. It handles customized handling of existing types and provide implementation for user-defined types.
FormData.__call__ does not take an encoding arg anymore and its return value changes from an iterator or bytes to a Payload instance. aiohttp provides payload adapters for some standard types like str, byte, io.IOBase, StreamReader or DataQueue.
a generator is not supported as data provider anymore, streamer can be used instead. For example, to upload data from file:
@aiohttp.streamer def file_sender(writer, file_name=None): with open(file_name, 'rb') as f: chunk = f.read(2**16) while chunk: yield from writer.write(chunk) chunk = f.read(2**16) # Then you can use `file_sender` like this: async with session.post('http://httpbin.org/post', data=file_sender(file_name='huge_file')) as resp: print(await resp.text())
the encoding parameter is deprecated in ClientSession.request(). Payload encoding is controlled at the payload level. It is possible to specify an encoding for each payload instance.
the version parameter is removed in ClientSession.request() client version can be specified in the ClientSession constructor.
aiohttp.MsgType dropped, use aiohttp.WSMsgType instead.
ClientResponse.url is an instance of yarl.URL class (url_obj is deprecated)
ClientResponse.json() is strict about response’s content type. if content type does not match, it raises
aiohttp.ClientResponseErrorexception. To disable content type check you can pass
Noneas content_type parameter.
ServerHttpProtocol and low-level details¶
Internal implementation was significantly redesigned to provide better performance and support HTTP pipelining. ServerHttpProtocol is dropped, implementation is merged with RequestHandler a lot of low-level api’s are dropped.
Constructor parameter loop is deprecated. Loop is get configured by application runner, run_app function for any of gunicorn workers.
Application.router.add_subapp is dropped, use Application.add_subapp instead
Application.finished is dropped, use Application.cleanup instead
WebRequest and WebResponse¶
the GET and POST attributes no longer exist. Use the query attribute instead of GET
Custom chunking size is not support WebResponse.chunked - developer is responsible for actual chunking.
Payloads are supported as body. So it is possible to use client response’s content object as body parameter for WebResponse
FileSender api is dropped, it is replaced with more general FileResponse class:
async def handle(request): return web.FileResponse('path-to-file.txt')
WebSocketResponse.protocol is renamed to WebSocketResponse.ws_protocol. WebSocketResponse.protocol is instance of RequestHandler class.
Reading request’s payload may raise a RequestPayloadError exception. The behavior is similar to ClientPayloadError.
WSGI support has been dropped, as well as gunicorn wsgi support. We still provide default and uvloop gunicorn workers for web.Application