Aiosonic
Aiosonic is a lightweight and very fast asynchronous HTTP/WebSocket client for Python, built on `asyncio`. It supports HTTP/1.1 and HTTP/2, offering features like connection pooling, multipart file uploads, chunked transfer handling, automatic decompression, and redirect following. Version 0.31.1 supports Python >= 3.10 and continues to evolve with a focus on performance and robust network interactions, including recent improvements for HTTP/2.
Warnings
- breaking Aiosonic version 0.30.0 dropped official support for Python 3.8 and 3.9. The minimum required Python version is now 3.10.
- deprecated The `AioSonicBaseClient` class was renamed to `BaseClient` in earlier versions (around 0.25.0/0.29.0). While a backward-compatible alias was restored in 0.30.1, it emits a `DeprecationWarning` and is scheduled for removal in the first 1.x release.
- gotcha As of version 0.29.0, `BaseClient` no longer automatically parses JSON responses. Users who wrapped APIs with `BaseClient` expecting automatic JSON parsing will need to adjust their implementation.
- gotcha In version 0.26.0, `response.json()` no longer strictly enforces the `Content-Type` header to be `application/json`. It will attempt to parse any valid JSON response regardless of the header.
Install
-
pip install aiosonic
Imports
- HTTPClient
from aiosonic import HTTPClient
- WebSocketClient
from aiosonic import WebSocketClient
- BaseClient
from aiosonic import BaseClient
Quickstart
import asyncio
import aiosonic
async def main():
client = aiosonic.HTTPClient()
try:
response = await client.get('https://httpbin.org/get')
assert response.status_code == 200
print(f"Status Code: {response.status_code}")
print(f"Response content: {await response.text()}")
finally:
# Ensure client is closed to release resources
await client.wait_requests_done()
if __name__ == '__main__':
asyncio.run(main())