Client session is the recommended interface for making HTTP requests.
Session encapsulates connection pool (connector instance) and supports keep-alives by default.
Usage example:
>>> import aiohttp
>>> session = aiohttp.ClientSession()
>>> resp = yield from session.get('http://python.org')
>>> resp
<ClientResponse(python.org/) [200]>
>>> data = yield from resp.read()
New in version 0.15.2.
The class for creating client sessions and making requests.
Parameters: |
|
---|
Changed in version 0.16: request_class default changed from None to ClientRequest
Changed in version 0.16: response_class default changed from None to ClientResponse
aiohttp.connector.BaseConnector derived instance used for the session.
A read-only property.
The session cookies, http.cookies.SimpleCookie instance.
A read-only property. Overriding session.cookies = new_val is forbidden, but you may modify the object inplace if needed.
Performs an asynchronous http request. Returns a response object.
Parameters: |
|
---|
Perform a GET request.
In order to modify inner request parameters, provide kwargs.
Parameters: |
---|
Perform a POST request.
In order to modify inner request parameters, provide kwargs.
Parameters: |
|
---|
Perform a PUT request.
In order to modify inner request parameters, provide kwargs.
Parameters: |
|
---|
Perform a DELETE request.
In order to modify inner request parameters, provide kwargs.
Parameters: | url (str) – Request URL |
---|
Perform a HEAD request.
In order to modify inner request parameters, provide kwargs.
Parameters: |
---|
Perform an OPTIONS request.
In order to modify inner request parameters, provide kwargs.
Parameters: |
---|
Perform a PATCH request.
In order to modify inner request parameters, provide kwargs.
Parameters: |
|
---|
Create a websocket connection. Returns a ClientWebSocketResponse object.
Parameters: |
|
---|
New in version 0.16.
Perform an asynchronous http request. Return a response object (ClientResponse or derived from).
Parameters: |
|
---|
Usage:
>>> import aiohttp
>>> resp = yield from aiohttp.request('GET', 'http://python.org/')
>>> resp
<ClientResponse(python.org/) [200]>
>>> data = yield from resp.read()
Connectors are transports for aiohttp client API.
There are standard connectors:
All connector classes should be derived from BaseConnector.
By default all connectors except ProxyConnector support keep-alive connections (behavior controlled by force_close constructor’s parameter).
Base class for all connectors.
Parameters: |
|
---|
Deprecated since version 0.15.2: share_cookies parameter is deprecated, use ClientSession for hadling cookies for client connections.
Read-only property, True if connector should ultimately close connections on releasing.
New in version 0.16.
The limit for simultaneous connections to the same endpoint.
Endpoints are the same if they are have equal (host, port, is_ssl) triple.
If limit is None the connector has no limit (default).
Read-only property.
New in version 0.16.
Get a free connection from pool or create new one if connection is absent in the pool.
The call may be paused if limit is exhausted until used connetions returns to pool.
Parameters: | request (aiohttp.client.ClientRequest) – request object which is connection initiator. |
---|---|
Returns: | Connection object. |
Connector for working with HTTP and HTTPS via TCP sockets.
The most common transport. When you don’t know what connector type to use, use a TCPConnector instance.
TCPConnector inherits from BaseConnector.
Constructor accepts all parameters suitable for BaseConnector plus several TCP-specific ones:
Parameters: |
|
---|
ssl.SSLContext instance for https requests, read-only property.
Use quick lookup in internal DNS cache for host names if True.
Read-only bool property.
Use quick lookup in internal DNS cache for host names if True.
Read-only bool property.
The cache of resolved hosts if resolve is enabled.
Read-only types.MappingProxyType property.
HTTP Proxy connector.
Use ProxyConnector for sending HTTP/HTTPS requests through HTTP proxy.
ProxyConnector is inherited from TCPConnector.
Usage:
>>> conn = ProxyConnector(proxy="http://some.proxy.com")
>>> session = ClientSession(connector=conn)
>>> resp = yield from session.get('http://python.org')
Constructor accepts all parameters suitable for TCPConnector plus several proxy-specific ones:
Parameters: |
|
---|
Note
ProxyConnector in opposite to all other connectors doesn’t support keep-alives by default (force_close is True).
Changed in version 0.16: force_close parameter changed to True by default.
Unix socket connector.
Use ProxyConnector for sending HTTP/HTTPS requests through UNIX Sockets as underlying transport.
UNIX sockets are handy for writing tests and making very fast connections between processes on the same host.
UnixConnector is inherited from BaseConnector.
Usage:
>>> conn = UnixConnector(path='/path/to/socket') >>> session = ClientSession(connector=conn) >>> resp = yield from session.get('http://python.org')
Constructor accepts all parameters suitable for BaseConnector plus unix-specific one:
Parameters: | path (str) – Unix socket path |
---|
Encapsulates single connection in connector object.
End user should never create Connection instances manually but get it by BaseConnector.connect() coroutine.