A lightweight asyncio HTTP client
Project description
bareClient
A simple asyncio http client.
Description
This package provides the asyncio transport for h11.
It makes little attempt to provide any helpful features.
Installation
This is a Python 3.7 package.
pip install bareclient
Usage
The basic usage is to create an HttpClient
.
import asyncio
from bareclient import HttpClient
import ssl
async def main(url, headers, ssl):
async with HttpClient(url, method='GET', headers=headers, ssl=ssl) as (response, body):
print(response)
if response.status_code == 200:
async for part in body():
print(part)
url = 'https://docs.python.org/3/library/cgi.html'
headers = [(b'host', b'docs.python.org'), (b'connection', b'close')]
ssl_context = ssl.SSLContext()
loop = asyncio.get_event_loop()
loop.run_until_complete(main(url, headers, ssl_context))
There is also an HttpSession
for keep-alive connections.
import asyncio
from bareclient import HttpSession
import ssl
async def main(url, headers, paths, ssl):
async with HttpSession(url, ssl=ssl) as requester:
for path in paths:
response, body = await requester.request(path, method='GET', headers=headers)
print(response)
if response.status_code == 200:
async for part in body():
print(part)
url = 'https://docs.python.org'
headers = [(b'host', b'docs.python.org'), (b'connection', b'keep-alive')]
paths = ['/3/library/cgi.html', '/3/library/urllib.parse.html']
ssl_context = ssl.SSLContext()
loop = asyncio.get_event_loop()
loop.run_until_complete(main(url, headers, paths, ssl_context))
Finally there is a single helper function to get json.
import asyncio
import ssl
from bareclient import get_json
async def main(url, ssl):
obj = await get_json(url, ssl=ssl)
print(obj)
url = 'https://jsonplaceholder.typicode.com/todos/1'
ssl_context = ssl.SSLContext()
loop = asyncio.get_event_loop()
loop.run_until_complete(main(url, ssl_context))
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
bareclient-2.0.0.tar.gz
(6.8 kB
view hashes)
Built Distribution
bareclient-2.0.0-py3-none-any.whl
(12.7 kB
view hashes)
Close
Hashes for bareclient-2.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75032d0d250d3da35dcff3c1bb372ba31d9b712e130dd7ba7274172ff4582d9b |
|
MD5 | 37fd399057fadcd3d48c206cc649c47e |
|
BLAKE2b-256 | 0bc93769231dec11211321620336361259cf4156a4ac3fa8471fb215a3759985 |