ClamAV Client

0.7.2 · active · verified Thu Apr 16

The clamav-client library (current version 0.7.2) provides a Python client for interacting with a ClamAV antivirus daemon, allowing applications to scan files and streams for viruses. It is actively maintained, with releases typically tied to feature updates, bug fixes, and Python version support.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `ClamAVClient`, ping the server to verify connectivity, and scan a local file using `scan_stream`. It includes error handling for common connection and client-specific issues.

import os
from clamav_client import ClamAVClient
from clamav_client.exceptions import ClamAVClientError

# Configure ClamAV server details, use environment variables for production
CLAMAV_HOST = os.environ.get('CLAMAV_HOST', '127.0.0.1')
CLAMAV_PORT = int(os.environ.get('CLAMAV_PORT', '3310'))
CLAMAV_TIMEOUT = float(os.environ.get('CLAMAV_TIMEOUT', '10.0'))

client = ClamAVClient(host=CLAMAV_HOST, port=CLAMAV_PORT, timeout=CLAMAV_TIMEOUT)

try:
    # Ping the ClamAV server to check connectivity
    version = client.ping()
    print(f"Connected to ClamAV server (version: {version})")

    # Create a dummy file for scanning
    dummy_file_path = "test_file.txt"
    with open(dummy_file_path, "w") as f:
        f.write("This is a clean test file.")

    # Scan a local file
    print(f"Scanning file: {dummy_file_path}")
    with open(dummy_file_path, "rb") as f:
        scan_result = client.scan_stream(f)

    if scan_result.is_infected:
        print(f"File '{dummy_file_path}' is infected! Virus: {scan_result.virus_name}")
    else:
        print(f"File '{dummy_file_path}' is clean.")

    # Clean up the dummy file
    os.remove(dummy_file_path)

except ClamAVClientError as e:
    print(f"Error communicating with ClamAV server: {e}")
except ConnectionRefusedError:
    print(f"Connection refused. Is ClamAV running at {CLAMAV_HOST}:{CLAMAV_PORT}?")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →