Python : module pyclamd
pyclamd.py
Author : Alexandre Norman - norman()xael.org
Contributors :
- PL : Philippe Lagadec - philippe.lagadec()laposte.net
- TK : Thomas Kastner - tk()underground8.com
- TT : Theodoropoulos Theodoros (TeD TeD) - sbujam()gmail.com
- TKL : Thomas Kluyver - thomas () kluyver.me.uk
Licence : LGPL
Usage :
Test strings :
^^^^^^^^^^^^
>>> import pyclamd
>>> try:
... cd = pyclamd.ClamdUnixSocket()
… # test if server is reachable
… cd.ping()
… except pyclamd.ConnectionError:
… # if failed, test for network socket
… cd = pyclamd.ClamdNetworkSocket()
… try:
… cd.ping()
… except pyclamd.ConnectionError:
… raise ValueError(‘could not connect to clamd server either by unix or network socket’)
True
>>> print(cd.version().split()[0])
ClamAV
>>> print(cd.reload())
RELOADING
>>> print(cd.stats().split()[0])
POOLS:
>>> void = open(‘/tmp/EICAR‘, ’w’).write(cd.EICAR())
>>> void = open(‘/tmp/NO_EICAR’, ’w’).write(‘no virus in this file’)
>>> cd.scan_file(‘/tmp/EICAR‘)[‘/tmp/EICAR‘]
(‘FOUND‘, ‘Eicar-Test-Signature’)
>>> cd.scan_file(‘/tmp/NO_EICAR’) is None
True
>>> cd.scan_stream(cd.EICAR())[‘stream’]
(‘FOUND‘, ‘Eicar-Test-Signature’)
>>> directory = cd.contscan_file(‘/tmp/’)
>>> directory[‘/tmp/EICAR‘]
(‘FOUND‘, ‘Eicar-Test-Signature’)
>>> # Testing encoding with non latin characters (Chinese ideograms taken from random site, don’t know what it mean, sorry)
>>> void = open(‘/tmp/EICAR-éèô请收藏我们的网址’, ’w’).write(cd.EICAR())
>>> r = cd.scan_file(‘/tmp/EICAR-éèô请收藏我们的网址’)
>>> print(list(r.keys())[0])
/tmp/EICAR-éèô请收藏我们的网址
>>> print(r[‘/tmp/EICAR-éèô请收藏我们的网址’])
(‘FOUND‘, ‘Eicar-Test-Signature’)
>>> import os
>>> os.remove(‘/tmp/EICAR‘)
>>> os.remove(‘/tmp/NO_EICAR’)
>>> os.remove(‘/tmp/EICAR-éèô请收藏我们的网址’)
Classes |
| |
- exceptions.ValueError(exceptions.StandardError)
-
- BufferTooLongError
- _ClamdGeneric(__builtin__.object)
-
- ClamdNetworkSocket
- ClamdUnixSocket
- socket.error(exceptions.IOError)
-
- ConnectionError
class ClamdNetworkSocket(_ClamdGeneric) |
|
Class for using clamd with a network socket |
|
- Method resolution order:
- ClamdNetworkSocket
- _ClamdGeneric
- __builtin__.object
Methods defined here:
- __init__(self, host=‘127.0.0.1’, port=3310, timeout=None)
- Network Class initialisation
host (string) : hostname or ip address
port (int) : TCP port
timeout (float or None) : socket timeout
Methods inherited from _ClamdGeneric:
- EICAR(self)
- returns Eicar test string
- contscan_file(self, file)
- Scan a file or directory given by filename
Do not stop on error or virus found.
Scan with archive support enabled.
file (string) : filename or directory (MUST BE ABSOLUTE PATH ! )
return either :
- (dict) : {filename1 : (‘FOUND‘, ‘virusname’), filename2 : (‘ERROR‘, ‘reason’)}
- None : if no virus found
May raise:
- ConnectionError: in case of communication problem
- multiscan_file(self, file)
- Scan a file or directory given by filename using multiple threads (faster on SMP machines).
Do not stop on error or virus found.
Scan with archive support enabled.
file (string) : filename or directory (MUST BE ABSOLUTE PATH ! )
return either :
- (dict) : {filename1 : (‘FOUND‘, ‘virusname’), filename2 : (‘ERROR‘, ‘reason’)}
- None : if no virus found
May raise:
- ConnectionError: in case of communication problem
- ping(self)
- Send a PING to the clamav server, which should reply
by a PONG.
return : True if the server replies to PING
May raise:
- ConnectionError: if the server do not reply by PONG
- reload(self)
- Force Clamd to reload signature database
return : (string) "RELOADING"
May raise:
- ConnectionError: in case of communication problem
- scan_file(self, file)
- Scan a file or directory given by filename and stop on first virus or error found.
Scan with archive support enabled.
file (string) : filename or directory (MUST BE ABSOLUTE PATH ! )
return either :
- (dict) : {filename1 : “virusname”}
- None : if no virus found
May raise :
- ConnectionError: in case of communication problem
- socket.timeout : if timeout has expired
- scan_stream(self, buffer_to_test)
- Scan a buffer
buffer_to_test (string) : buffer to scan
return either:
- (dict) : {filename1 : "virusname"}
- None : if no virus found
May raise :
- BufferTooLongError: if the buffer size exceeds clamd limits
- ConnectionError: in case of communication problem
- shutdown(self)
- Force Clamd to shutdown and exit
return : nothing
May raise:
- ConnectionError: in case of communication problem
- stats(self)
- Get Clamscan stats
return : (string) clamscan stats
May raise:
- ConnectionError: in case of communication problem
- version(self)
- Get Clamscan version
return : (string) clamscan version
May raise:
- ConnectionError: in case of communication problem
Data descriptors inherited from _ClamdGeneric:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
class ClamdUnixSocket(_ClamdGeneric) |
|
Class for using clamd with an unix socket |
|
- Method resolution order:
- ClamdUnixSocket
- _ClamdGeneric
- __builtin__.object
Methods defined here:
- __init__(self, filename=None, timeout=None)
- Unix Socket Class initialisation
filename (string) : unix socket filename or None to get the socket from /etc/clamav/clamd.conf
timeout (float or None) : socket timeout
Methods inherited from _ClamdGeneric:
- EICAR(self)
- returns Eicar test string
- contscan_file(self, file)
- Scan a file or directory given by filename
Do not stop on error or virus found.
Scan with archive support enabled.
file (string) : filename or directory (MUST BE ABSOLUTE PATH ! )
return either :
- (dict) : {filename1 : (‘FOUND‘, ‘virusname’), filename2 : (‘ERROR‘, ‘reason’)}
- None : if no virus found
May raise:
- ConnectionError: in case of communication problem
- multiscan_file(self, file)
- Scan a file or directory given by filename using multiple threads (faster on SMP machines).
Do not stop on error or virus found.
Scan with archive support enabled.
file (string) : filename or directory (MUST BE ABSOLUTE PATH ! )
return either :
- (dict) : {filename1 : (‘FOUND‘, ‘virusname’), filename2 : (‘ERROR‘, ‘reason’)}
- None : if no virus found
May raise:
- ConnectionError: in case of communication problem
- ping(self)
- Send a PING to the clamav server, which should reply
by a PONG.
return : True if the server replies to PING
May raise:
- ConnectionError: if the server do not reply by PONG
- reload(self)
- Force Clamd to reload signature database
return : (string) "RELOADING"
May raise:
- ConnectionError: in case of communication problem
- scan_file(self, file)
- Scan a file or directory given by filename and stop on first virus or error found.
Scan with archive support enabled.
file (string) : filename or directory (MUST BE ABSOLUTE PATH ! )
return either :
- (dict) : {filename1 : “virusname”}
- None : if no virus found
May raise :
- ConnectionError: in case of communication problem
- socket.timeout : if timeout has expired
- scan_stream(self, buffer_to_test)
- Scan a buffer
buffer_to_test (string) : buffer to scan
return either:
- (dict) : {filename1 : "virusname"}
- None : if no virus found
May raise :
- BufferTooLongError: if the buffer size exceeds clamd limits
- ConnectionError: in case of communication problem
- shutdown(self)
- Force Clamd to shutdown and exit
return : nothing
May raise:
- ConnectionError: in case of communication problem
- stats(self)
- Get Clamscan stats
return : (string) clamscan stats
May raise:
- ConnectionError: in case of communication problem
- version(self)
- Get Clamscan version
return : (string) clamscan version
May raise:
- ConnectionError: in case of communication problem
Data descriptors inherited from _ClamdGeneric:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
|
Functions |
| |
- contscan_file = wrapper(*args, **kw)
- Deprecated API - use one of the Clamd*Socket classes instead.
- init_network_socket(host=‘127.0.0.1’, port=3310, timeout=None)
- Deprecated API - use ClamdNetworkSocket instead.
- init_unix_socket(filename=None)
- Deprecated API - use ClamdUnixSocket instead.
- multiscan_file = wrapper(*args, **kw)
- Deprecated API - use one of the Clamd*Socket classes instead.
- scan_file = wrapper(*args, **kw)
- Deprecated API - use one of the Clamd*Socket classes instead.
- version = wrapper(*args, **kw)
- Deprecated API - use one of the Clamd*Socket classes instead.
|
Data |
| |
__version__ = ‘0.3.3’
socketinst = None |