A connection-aware, cache-control respecting http/2 over tls client
Project description
H2Client
This library is intended to provide improved control when writing an http/2
client without adding a significant amount of boilerplate. In particular this
allows for easy connection management, since when an http/2 connection is opened
and closed will have a very significant impact on performance, and respects
Cache-Control headers (request and response) using diskcached
, since
respecting these headers is often an insane performance improvement as it will
prevent a large number of requests from requiring any network calls at all.
This does not support ETag style validation as it doesn't tend to result in any significant improvements on REST api's where even the largest responses are measured in kilobytes since ETags do not prevent network calls.
Getting Started
python -m venv venv
. venv/bin/activate # for windows, "venv/Scripts/activate.bat"
python -m pip install --upgrade pip
pip install h2client
"""Make some requests using the postman-echo service run by
https://www.postman.com/. h2client is not in any way affiliated with or
endorsed by Postman.
"""
import asyncio
from h2client.session import Session
import json
USER_AGENT = 'H2ClientExamples/1 by /u/Tjstretchalot (+https://github.com/tjstretchalot/h2client)'
async def main():
client = Session(
'postman-echo.com',
default_headers={
'user-agent': USER_AGENT,
'accept': 'application/json'
}
)
async with client.conn() as conn:
responses = await asyncio.gather(
conn.get('/get', params={'foo1': 'bar1'}),
conn.post('/post', json={'payload': 'asdf'}),
conn.delete('/delete', data=b'look some data!', headers={
'content-type': 'text/plain'
})
)
for response in responses:
print(f'{response.method} {response.path} - {response.status_code} {response.status_text}')
print('Request Headers:')
for key, val in response.request_headers.items():
print(f' {key}: {val}')
print('Headers:')
for key, val in response.headers.items():
print(f' {key}: {val}')
print()
print('Body (interpreted):')
print(json.dumps(response.json(), indent=2, sort_keys=True))
if __name__ == '__main__':
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(main())
# Wait for the `ensure_future`s to finish cleanly
pending = asyncio.all_tasks(loop)
while pending:
loop.run_until_complete(asyncio.wait(pending, return_when=asyncio.ALL_COMPLETED))
pending = asyncio.all_tasks(loop)
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
Built Distribution
File details
Details for the file h2client-0.2.3.tar.gz
.
File metadata
- Download URL: h2client-0.2.3.tar.gz
- Upload date:
- Size: 18.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17f6e29a274db8d9b4d05806daf5093d7ff987c58ee768caa6b52c488a3271db |
|
MD5 | 0e91be6d78521ee0bc4b56f7191134c2 |
|
BLAKE2b-256 | 198598a53d44a0be5fde70ab34dc8592b889ecef61df13b7d67a31479461650a |
File details
Details for the file h2client-0.2.3-py3-none-any.whl
.
File metadata
- Download URL: h2client-0.2.3-py3-none-any.whl
- Upload date:
- Size: 21.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ec31f5055e622ff1e86d44d0fb0817cfe3677566d03570b2b115ad6847f2e96 |
|
MD5 | 8e171d556ddb70fb59f0a71af187b438 |
|
BLAKE2b-256 | 514001a31567dd92b00195b762d01ab61a453287db928573f58ac2eb388996fe |