clamd is a portable Python module to use the ClamAV anti-virus engine on
Windows, Linux, macOS and other platforms. It requires a running instance of
the clamd daemon.
This is a fork of https://github.com/graingert/python-clamd
Original credits:
- pyClamd v0.2.0 by Philippe Lagadec (http://www.decalage.info/en/python/pyclamd)
- pyClamd v0.1.1 by Alexandre Norman (http://xael.org/norman/python/pyclamd/)
Install from PyPI:
pip install clamdOr install from source:
git clone https://github.com/arasemco/python-clamd.git
cd python-clamd
pip install -e .- Python 3.6 or higher
- Running ClamAV daemon (clamd)
Install ClamAV daemon on Ubuntu:
sudo apt-get install clamav-daemon clamav-freshclam
sudo freshclam
sudo service clamav-daemon startUnix socket connection:
>>> import clamd
>>> cd = clamd.ClamdUnixSocket()
>>> cd.ping()
'PONG'
>>> cd.version()
'ClamAV ...'
>>> cd.reload()
'RELOADING'Network socket connection:
>>> cd = clamd.ClamdNetworkSocket(host='127.0.0.1', port=3310)Scan a file:
>>> with open('/tmp/EICAR', 'wb') as f:
... f.write(clamd.EICAR)
>>> cd.scan('/tmp/EICAR')
{'/tmp/EICAR': ('FOUND', 'Eicar-Test-Signature')}Scan a stream (buffer):
>>> from io import BytesIO
>>> cd.instream(BytesIO(clamd.EICAR))
{'stream': ('FOUND', 'Eicar-Test-Signature')}Scan a directory recursively:
>>> results = cd.multiscan('/path/to/directory')
>>> for path, (status, virus) in results.items():
... if status == 'FOUND':
... print(f'Virus found: {virus} in {path}')Get clamd statistics:
>>> print(cd.stats())Classes:
ClamdUnixSocket(path='/var/run/clamav/clamd.ctl', timeout=None)ClamdNetworkSocket(host='127.0.0.1', port=3310, timeout=None)
Both classes derive from the common BaseClamdSocket abstract base class.
Methods:
ping()- Check if clamd is respondingversion()- Get clamd versionreload()- Reload virus definitionsscan(file)- Scan a single filecontscan(file)- Continuous scan (don't stop on error)multiscan(file)- Multi-threaded directory scaninstream(buffer)- Scan a byte streamstats()- Get clamd statisticsshutdown()- Shutdown clamd daemon
Exceptions:
ConnectionError- Connection to clamd failedResponseError- Invalid response from clamdBufferTooLongError- Stream exceeds max length
The test suite runs against a live clamd daemon and is orchestrated with Docker Compose.
Run the full test matrix:
docker compose run --rm --build python-clamd-testRun against a specific Python version:
PYTHON_VERSION=3.12 docker compose run --rm --build python-clamd-testRun a single test:
PYTHON_VERSION=3.12 docker compose run --rm --build python-clamd-test \
pytest -sv test/test_clamd/test_api.py::TestClamdUnixSocket::test_pingStart a development container with the source mounted and dependencies installed:
PYTHON_VERSION=3.12 docker compose run --rm --build python-clamd-devclamd is released as open-source software under the GNU Lesser General Public License v2.1 or later (LGPL-2.1-or-later).
Issues and pull requests are welcome at: https://github.com/arasemco/python-clamd
See CHANGES.md for version history.
