pyclamd (version 0.3.9) | index /home/xael/ESPACE_KM/python/pyclamd/pyclamd.py

`pyclamd.py
Author : Alexandre Norman - norman()xael.org
Contributors :
- JB : Joe Brandt - brandt.joe () gmail.com
- JS : Joni Salonen - joni.salonen () qindel.com
- PL : Philippe Lagadec - philippe.lagadec()laposte.net
- SK : Scott Kitterman - debian () kitterman.com
- TK : Thomas Kastner - tk()underground8.com
- TKL : Thomas Kluyver - thomas () kluyver.me.uk
- TT : Theodoropoulos Theodoros (TeD TeD) - sbujam()gmail.com
Licence : LGLPv3+
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-éèô请收藏我们的网址')`

Modules

``

base64 | socket | struct |

Classes

``

exceptions.ValueError(exceptions.StandardError)
  BufferTooLongError
_ClamdGeneric(__builtin__.object)
  ClamdNetworkSocket
  ClamdUnixSocket
socket.error(exceptions.IOError)
  ConnectionError

class BufferTooLongError(exceptions.ValueError)
`` | `Class for errors with clamd using INSTREAM with a buffer lenght > StreamMaxLength in /etc/clamav/clamd.conf`

Method resolution order:
  BufferTooLongError
  exceptions.ValueError
  exceptions.StandardError
  exceptions.Exception
  exceptions.BaseException
  __builtin__.object

Data descriptors defined here:

__weakref__
  `list of weak references to the object (if defined)`

Methods inherited from exceptions.ValueError:

__init__(...)
  `x.__init__(...) initializes x; see help(type(x)) for signature`

Data and other attributes inherited from exceptions.ValueError:

__new__ = <built-in method __new__ of type object>
  `T.__new__(S, ...) -> a new object with type S, a subtype of T`

Methods inherited from exceptions.BaseException:

__delattr__(...)
  `x.__delattr__('name') <==> del x.name`

__getattribute__(...)
  `x.__getattribute__('name') <==> x.name`

__getitem__(...)
  `x.__getitem__(y) <==> x[y]`

__getslice__(...)
  `x.__getslice__(i, j) <==> x[i:j]
  Use of negative indices is not supported.`

__reduce__(...)

__repr__(...)
  `x.__repr__() <==> repr(x)`

__setattr__(...)
  `x.__setattr__('name', value) <==> x.name = value`

__setstate__(...)

__str__(...)
  `x.__str__() <==> str(x)`

__unicode__(...)

Data descriptors inherited from exceptions.BaseException:

__dict__

args

message

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)`

class ConnectionError(socket.error)
`` | `Class for errors communication with clamd`

Method resolution order:
  ConnectionError
  socket.error
  exceptions.IOError
  exceptions.EnvironmentError
  exceptions.StandardError
  exceptions.Exception
  exceptions.BaseException
  __builtin__.object

Data descriptors inherited from socket.error:

__weakref__
  `list of weak references to the object (if defined)`

Methods inherited from exceptions.IOError:

__init__(...)
  `x.__init__(...) initializes x; see help(type(x)) for signature`

Data and other attributes inherited from exceptions.IOError:

__new__ = <built-in method __new__ of type object>
  `T.__new__(S, ...) -> a new object with type S, a subtype of T`

Methods inherited from exceptions.EnvironmentError:

__reduce__(...)

__str__(...)
  `x.__str__() <==> str(x)`

Data descriptors inherited from exceptions.EnvironmentError:

errno
  `exception errno`

filename
  `exception filename`

strerror
  `exception strerror`

Methods inherited from exceptions.BaseException:

__delattr__(...)
  `x.__delattr__('name') <==> del x.name`

__getattribute__(...)
  `x.__getattribute__('name') <==> x.name`

__getitem__(...)
  `x.__getitem__(y) <==> x[y]`

__getslice__(...)
  `x.__getslice__(i, j) <==> x[i:j]
  Use of negative indices is not supported.`

__repr__(...)
  `x.__repr__() <==> repr(x)`

__setattr__(...)
  `x.__setattr__('name', value) <==> x.name = value`

__setstate__(...)

__unicode__(...)

Data descriptors inherited from exceptions.BaseException:

__dict__

args

message

Functions

``

ClamdAgnostic()
  `Tries to connect to clamd using ClamdUnixSocket or if it fails, tries
  with ClamdNetworkSocket and return the corresponding object.
  Of course, it tries to connect with default settings...`

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.9' socketinst = None