Skip to content

sonic182/aiosonic

Repository files navigation

github status Coverage Status PyPI version Documentation Status Discord

aiosonic - lightweight Python asyncio http client

Very fast, lightweight Python asyncio http client

Here is some documentation.

There is a performance script in tests folder which shows very nice numbers

» python tests/performance.py
doing tests...
{
 "aiosonic": "1000 requests in 182.03 ms",
 "aiosonic cyclic": "1000 requests in 370.55 ms",
 "aiohttp": "1000 requests in 367.66 ms",
 "requests": "1000 requests in 4613.77 ms",
 "httpx": "1000 requests in 812.41 ms"
}
aiosonic is 101.97% faster than aiohttp
aiosonic is 2434.55% faster than requests
aiosonic is 103.56% faster than aiosonic cyclic
aiosonic is 346.29% faster than httpx

This is a very basic, dummy test, machine dependant. If you look for performance, test and compare your code with this and other packages like aiohttp.

You can perform this test by installing all test dependencies with pip install -e ".[test]" and doing python tests/performance.py in your own machine

Requirements:

  • Python>=3.7
  • PyPy>=3.7

Features:

  • Keepalive and smart pool of connections
  • Multipart File Uploads
  • Chunked responses handling
  • Chunked requests
  • Connection Timeouts
  • Automatic Decompression
  • Follow Redirects
  • Fully type annotated.
  • 100% test coverage (Sometimes not).
  • HTTP2 (BETA) when using the correct flag

Installation

pip install aiosonic

Usage

import asyncio
import aiosonic
import json


async def run():
    client = aiosonic.HTTPClient()

    # ##################
    # Sample get request
    # ##################
    response = await client.get('https://www.google.com/')
    assert response.status_code == 200
    assert 'Google' in (await response.text())

    # ##################
    # Post data as multipart form
    # ##################
    url = "https://postman-echo.com/post"
    posted_data = {'foo': 'bar'}
    response = await client.post(url, data=posted_data)

    assert response.status_code == 200
    data = json.loads(await response.content())
    assert data['form'] == posted_data

    # ##################
    # Posted as json
    # ##################
    response = await client.post(url, json=posted_data)

    assert response.status_code == 200
    data = json.loads(await response.content())
    assert data['json'] == posted_data

    # ##################
    # Sample request + timeout
    # ##################
    from aiosonic.timeout import Timeouts
    timeouts = Timeouts(
        sock_read=10,
        sock_connect=3
    )
    response = await client.get('https://www.google.com/', timeouts=timeouts)
    assert response.status_code == 200
    assert 'Google' in (await response.text())
    await client.shutdown()

    print('success')


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())
  • HTTP2
    • Get
    • Request with data sending
    • Do a aiosonic release with stable http2
  • Better documentation
  • International Domains and URLs (idna + cache)
  • Basic/Digest Authentication
  • Requests using a http proxy
  • Sessions with Cookie Persistence
  • Elegant Key/Value Cookies

Development

Install packages with pip-tools:

pip install pip-tools
pip-compile
pip-compile test-requirements.in
pip-sync requirements.txt test-requirements.txt

Contribute

  1. Fork
  2. create a branch feature/your_feature
  3. commit - push - pull request

Thanks :)

Contributors