Python client for the Contree API, generated from the OpenAPI specification
Actively maintained. Last release 3 days ago, commits in 14 of the last 26 weeks. A safe choice.
pip install contree-clientcontree-client
Python client for the Contree API — sandboxed code execution combining virtual machine isolation with container operational efficiency.
Synchronous clients are named ContreeClient, asynchronous ones are
named ContreeAsyncClient; every backend shares the same interface:
| Backend | Extra | Import |
|---|---|---|
| http.client | — (stdlib) | from contree_client.http import ContreeClient |
| urllib3 | contree-client[urllib3] | from contree_client.urllib3 import ContreeClient |
| requests | contree-client[requests] | from contree_client.requests import ContreeClient |
| httpx | contree-client[httpx] | from contree_client.httpx import ContreeClient / ContreeAsyncClient |
| aiohttp | contree-client[aiohttp] | from contree_client.aiohttp import ContreeAsyncClient |
Or let the package pick the first installed backend:
from contree_client.sync import ContreeClient
from contree_client.asyncio import ContreeAsyncClient
Quick start
from contree_client.sync import ContreeClient
# reuse a saved Contree profile,
# or pass credentials directly: ContreeClient("IAM_TOKEN", project=...)
with ContreeClient.from_profile() as client:
response = client.spawn_instance(command="uname -a", image="tag:ubuntu:latest", shell=True)
for event in client.iter_operation_events(response.uuid, follow=True):
print(event.type, event.data)
Use cases
Run a command and read its output:
from contree_client import OperationStatus
response = client.spawn_instance(
command="echo hello",
image="tag:ubuntu:latest",
shell=True,
disposable=True, # do not persist a result image
timeout=60,
)
operation = client.wait_operation(response.uuid)
assert operation.status is OperationStatus.SUCCESS
print(operation.metadata.result.stdout.as_text())
Follow the live event stream with transparent reconnection
(Last-Event-Id resume; ends on the completion event):
import sys
from contree_client import decode_chunk
for event in client.follow_operation_events(response.uuid):
if event.type in ("stdout", "stderr"):
sys.stdout.buffer.write(decode_chunk(event.data))