Skip to main content

Lightweight Python asyncio HTTP client

Project description

lowhaio CircleCI Test Coverage

A lightweight Python asyncio HTTP/1.1 client. No additional tasks are created; all code is in a single module; and other than the standard library only a single dependency is required, aiodnsresolver

Lowhaio has a deliberately limited scope: it includes just enough code to be a useful HTTP client and allow more complex behaviour to be added on top if required.

Connections are DNS-aware, in that they are only re-used if they match a current A record for the domain.

Installation

pip install lowhaio

Usage

The API is streaming-first: for both request and response bodies, asynchronous iterators are used.

import asyncio
from lowhaio import Pool

async def main():
    request, close = Pool()

    async def request_body():
        yield b'a'
        yield b'bc'

    code, headers, response_body = await request(
        b'POST', 'https://postman-echo.com/post',
        headers=((b'content-length', b'3'), (b'content-type', b'text/plain'),),
        body=request_body,
    )
    async for chunk in response_body:
        print(chunk)

    await close()

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

However, there are helper functions streamed and buffered when this isn't required or possible.

import asyncio
from lowhaio import Pool, streamed, buffered

async def main():
    request, close = Pool()

    request_body = streamed(b'abc')

    code, headers, response_body = await request(
        b'POST', 'https://postman-echo.com/post',
        headers=((b'content-length', b'3'), (b'content-type', b'text/plain'),),
        body=request_body,
    )
    print(await buffered(response_body))

    await close()

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Headers

The only header automatically added to requests is the host header, which is the idna/punycode-encoded domain name from the requested URL.

Exceptions

Exceptions are usually subclasses of HttpError. If a lower-level exception caused this, it is set in the __cause__ attribute of the HttpError. Specifically, before any data is sent HttpConnectionError is raised, and after data is sent, HttpDataError is raised. This difference is to make it possible to know if non-idempotent requests can be retried.

However, asyncio.CancelledError and exceptions that do not directive from Exception, such as SystemExit, are allowed to bubble up.

Custom SSL context

Lowhaio can be used with an custom SSL context through through the get_ssl_context parameter to Pool. For example, to use the certifi CA bundle, you can install it by

pip install certifi

and use it as below.

import asyncio
import ssl

import certifi
from lowhaio import Pool, buffered, streamed

async def main():
    request, close = Pool(
        get_ssl_context=lambda: ssl.create_default_context(cafile=certifi.where()),
    )

    request_body = streamed(b'abc')

    code, headers, response_body = await request(
        b'POST', 'https://postman-echo.com/post',
        headers=((b'content-length', b'3'), (b'content-type', b'text/plain'),),
        body=request_body,
    )
    print(await buffered(response_body))

    await close()

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Scope

The scope of the core functions is restricted to:

  • (TLS) connection opening, closing and pooling;
  • passing and receiving HTTP headers and streaming bodies;
  • decoding chunked responses;
  • raising exceptions on timeouts.

This is to make the core behaviour useful to a reasonable range of uses, but to not include what can be added by layer(s) on top. Specifically not included:

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lowhaio-0.0.84.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

lowhaio-0.0.84-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file lowhaio-0.0.84.tar.gz.

File metadata

  • Download URL: lowhaio-0.0.84.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.22.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for lowhaio-0.0.84.tar.gz
Algorithm Hash digest
SHA256 1a537c16127b8c91875dcfd5dad2f92e66fb3023db329453815faa9fbae6b378
MD5 eb36f297609384cc5538e66d3cfc8f89
BLAKE2b-256 4d23e860aec59aaa43d958f37276423269b59cd991491d1772a3fb5bb5454869

See more details on using hashes here.

File details

Details for the file lowhaio-0.0.84-py3-none-any.whl.

File metadata

  • Download URL: lowhaio-0.0.84-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.22.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for lowhaio-0.0.84-py3-none-any.whl
Algorithm Hash digest
SHA256 0f28dcb8225e2aec90adbff8c92715add81a1db923817cfb2c085b282b8c19ea
MD5 1ec14732f8d083b30b092b20cb154e6d
BLAKE2b-256 7b443a18ed1c2cef11e36781514b872f254663013e9262eedf5c6d10e0ae50ba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page