Skip to main content

Python SDK for the ClamAV API service (REST and gRPC)

Project description

clamav-sdk

Python SDK for the ClamAV API service. Supports both REST (HTTP/JSON) and gRPC transports with synchronous and asynchronous interfaces.

Installation

pip install clamav-sdk

For async REST support (uses httpx):

pip install clamav-sdk[async]

Quick Start — REST Client

from clamav_sdk import ClamAVClient

client = ClamAVClient("http://localhost:6000")

# Health check
health = client.health_check()
print(health.healthy, health.message)

# Server version
info = client.version()
print(f"{info.version} ({info.commit})")

# Scan a file on disk
result = client.scan_file("/path/to/file.pdf")
print(result.status, result.message, result.scan_time)

# Scan in-memory bytes
result = client.scan_bytes(b"file content", filename="doc.txt")

# Scan via binary stream endpoint
result = client.scan_stream(b"raw bytes")

Quick Start — gRPC Client

from clamav_sdk import ClamAVGRPCClient

with ClamAVGRPCClient("localhost:9000") as client:
    # Health check
    health = client.health_check()

    # Scan file bytes (unary RPC)
    result = client.scan_file(open("sample.bin", "rb").read(), filename="sample.bin")
    print(result.status)

    # Scan via streaming RPC (automatic chunking)
    result = client.scan_stream(large_payload, filename="big.zip", chunk_size=65536)

    # Scan multiple files over a single bidirectional stream
    files = [
        ("report.pdf", open("report.pdf", "rb")),
        ("image.png", open("image.png", "rb")),
    ]
    results = client.scan_multiple(files)
    for r in results:
        print(f"{r.filename}: {r.status}")

Async REST Client

Requires the async extra (pip install clamav-sdk[async]).

import asyncio
from clamav_sdk import AsyncClamAVClient

async def main():
    async with AsyncClamAVClient("http://localhost:6000") as client:
        health = await client.health_check()
        result = await client.scan_file("/path/to/file.pdf")
        print(result.status)

asyncio.run(main())

Async gRPC Client

import asyncio
from clamav_sdk import AsyncClamAVGRPCClient

async def main():
    async with AsyncClamAVGRPCClient("localhost:9000") as client:
        result = await client.scan_file(b"payload", filename="test.bin")
        print(result.status)

        # scan_multiple yields results as they arrive
        files = [("a.txt", b"aaa"), ("b.txt", b"bbb")]
        async for r in client.scan_multiple(files):
            print(f"{r.filename}: {r.status}")

asyncio.run(main())

TLS / Secure Channels

import grpc
from clamav_sdk import ClamAVGRPCClient

creds = grpc.ssl_channel_credentials(
    root_certificates=open("ca.pem", "rb").read(),
)
client = ClamAVGRPCClient("secure-host:9000", credentials=creds)

Custom Session / Authentication

import requests
from clamav_sdk import ClamAVClient

session = requests.Session()
session.headers["Authorization"] = "Bearer <token>"
client = ClamAVClient("http://localhost:6000", session=session)

Exception Handling

All SDK methods raise exceptions from a unified hierarchy:

from clamav_sdk import ClamAVClient
from clamav_sdk.exceptions import (
    ClamAVError,                    # base
    ClamAVConnectionError,          # server unreachable
    ClamAVTimeoutError,             # scan timed out (HTTP 504 / gRPC DEADLINE_EXCEEDED)
    ClamAVServiceUnavailableError,  # ClamAV daemon down (HTTP 502 / gRPC INTERNAL)
    ClamAVFileTooLargeError,        # file exceeds size limit (HTTP 413)
    ClamAVBadRequestError,          # malformed request (HTTP 400)
)

client = ClamAVClient("http://localhost:6000")
try:
    result = client.scan_file("huge.iso")
except ClamAVFileTooLargeError:
    print("File exceeds server limit")
except ClamAVTimeoutError:
    print("Scan took too long")
except ClamAVError as exc:
    print(f"Unexpected error: {exc}")

Models

Model Fields
ScanResult status, message, scan_time, filename
HealthCheckResult healthy, message
VersionInfo version, commit, build

Development

git clone https://github.com/DevHatRo/clamav-api-sdk-python.git
cd clamav-api-sdk-python
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pytest --cov=clamav_sdk

License

MIT

Project details


Download files

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

Source Distribution

clamav_sdk-0.2.0.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

clamav_sdk-0.2.0-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file clamav_sdk-0.2.0.tar.gz.

File metadata

  • Download URL: clamav_sdk-0.2.0.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for clamav_sdk-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2930fab0c44e489c75da78eeb80e2b29bedcf94f252f5bb90fff2feb93895a42
MD5 ee09a8187052274761adb0b8fc7899ac
BLAKE2b-256 4cbdc227837955e8ab4dc5da71a81e0f48c2e2f39cd71f38e89365f0a76641b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for clamav_sdk-0.2.0.tar.gz:

Publisher: publish.yml on DevHatRo/clamav-api-sdk-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file clamav_sdk-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: clamav_sdk-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for clamav_sdk-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6410a11475d0a183e408f6e19c0814196c08b7e34b75992ff94a1f691d93c9ba
MD5 3af4f3d0aa736b248bfa95f55eafffc0
BLAKE2b-256 7a9a03f4b5cc92c1b2994791f845233e2e87d1cfc3da3e6e3e7139c6a999ecf8

See more details on using hashes here.

Provenance

The following attestation bundles were made for clamav_sdk-0.2.0-py3-none-any.whl:

Publisher: publish.yml on DevHatRo/clamav-api-sdk-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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