Clamd Python Interface

1.0.2 · active · verified Wed Apr 15

Clamd is a Python interface library for communicating with the ClamAV daemon (`clamd`). It allows Python applications to send files or streams to `clamd` for virus scanning. The current stable version is 1.0.2, with a low release cadence indicating maturity and stability, focusing on Python 3.7+.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to the ClamAV daemon using a Unix domain socket, send a test string for scanning, and interpret the results. It includes basic error handling for common connection issues.

import clamd
import os

# Assuming clamd is running and accessible via its default Unix socket
# You might need to adjust the socket path or use ClamdNetworkSocket
# if clamd is running on a network port.

def scan_string(text_to_scan: str):
    try:
        # Common Unix socket paths for clamd:
        # /var/run/clamav/clamd.ctl (Ubuntu/Debian)
        # /run/clamd.ctl (Arch Linux)
        # /var/lib/clamav/clamd.sock
        
        # Use environment variable for socket path or default
        clamd_socket_path = os.environ.get('CLAMD_UNIX_SOCKET', '/var/run/clamav/clamd.ctl')
        cd = clamd.ClamdUnixSocket(path=clamd_socket_path)
        
        # Test connection
        cd.ping()
        print(f"Successfully connected to clamd at {clamd_socket_path}")
        
        # Scan the string. The 'stream' key is arbitrary for stream scans.
        # 'EICAR_Test_File' is a safe test virus string.
        result = cd.scan(b'X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*')
        
        print(f"Scan result for string: {result}")
        
        # Interpret result
        if 'stream' in result and result['stream'][0] == 'FOUND':
            print(f"!!! VIRUS DETECTED: {result['stream'][1]} !!!")
        elif 'stream' in result and result['stream'][0] == 'OK':
            print("No virus detected.")
        else:
            print("Unexpected scan result format.")
            
    except clamd.ClamdError as e:
        print(f"Error connecting to or communicating with clamd: {e}")
        print("Please ensure the clamd daemon is running and accessible at the specified socket path/address, and that your user has permissions.")
    except FileNotFoundError:
        print(f"Error: Clamd Unix socket not found at {clamd_socket_path}. Please check the path and clamd status.")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

if __name__ == "__main__":
    # Example usage with the EICAR test string
    scan_string("This is a test file for antivirus scanning.")

view raw JSON →