python实现集中式的病毒扫描功能详解

yipeiwu_com5年前Python基础

本文实例讲述了python实现集中式的病毒扫描功能。分享给大家供大家参考,具体如下:

一 点睛

本次实践实现了一个集中式的病毒扫描管理,可以针对不同业务环境定制扫描策略,比如扫描对象、描述模式、扫描路径、调度频率等。案例实现的架构图如下,首先业务服务器开启clamd服务(监听3310端口),管理服务器启用多线程对指定的服务集群进行扫描,扫描模式、扫描路径会传递到clamd,最后返回扫描结果给管理服务器端。

 

本次实战通过ClamdNetworkSocket()方法实现与业务服务器建立扫描socket连接,再通过启动不同扫描方式实施病毒扫描并返回结果。

二 代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import pyclamd
from threading import Thread
class Scan(Thread):
  def __init__ (self,IP,scan_type,file):
    """构造方法"""
    Thread.__init__(self)
    self.IP = IP
    self.scan_type=scan_type
    self.file = file
    self.connstr=""
    self.scanresult=""
  def run(self):
    """多进程run方法"""
    try:
      cd = pyclamd.ClamdNetworkSocket(self.IP,3310)
      if cd.ping():
        self.connstr=self.IP+" connection [OK]"
        cd.reload()
        if self.scan_type=="contscan_file":
          self.scanresult="{0}\n".format(cd.contscan_file(self.file))
        elif self.scan_type=="multiscan_file":
          self.scanresult="{0}\n".format(cd.multiscan_file(self.file))
        elif self.scan_type=="scan_file":
          self.scanresult="{0}\n".format(cd.scan_file(self.file))
        time.sleep(1)
      else:
        self.connstr=self.IP+" ping error,exit"
        return
    except Exception,e:
      self.connstr=self.IP+" "+str(e)
IPs=['192.168.0.120']
scantype="multiscan_file"
scanfile="/data"
i=1
threadnum=2
scanlist = []
for ip in IPs:
  currp = Scan(ip,scantype,scanfile)
  scanlist.append(currp)
  if i%threadnum==0 or i==len(IPs):
    for task in scanlist:
      task.start()
    for task in scanlist:
      task.join()
      print task.connstr
      print task.scanresult
    scanlist = []
  i+=1

三 结果

1 无病毒的情况下,扫描结果

E:\Python\python_auto_maintain\venv\Scripts\python.exe E:/Python/python_auto_maintain/4_1_2.py
192.168.0.120 connection [OK]
None

2 有病毒的情况下,扫描结果

2.1 制作病毒测试文件

X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*

2.2 扫描结果

E:\Python\python_auto_maintain\venv\Scripts\python.exe E:/Python/python_auto_maintain/4_1_2.py
192.168.0.120 connection [OK]
{u'/data/EICAR': ('FOUND', 'Eicar-Test-Signature')}

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python进程与线程操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

wxPython 入门教程

wxPython 入门教程

这篇文章是关于 wxPython,但 wxPython 实际是两件事物的组合体:Python 脚本语言和 GUI 功能的 wxWindows 库(关于 wxWindows 的介绍,请参阅...

Python3 中作为一等对象的函数解析

Python3 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。 函数能提高应用的模块性,和代码的重复利用率。你已经知道Python提供了许多内建函数,比如pr...

Python WEB应用部署的实现方法

Python WEB应用部署的实现方法

本文介绍了Python WEB应用部署的实现方法,分享给大家,具体如下: 使用Apache模块mod_wsgi运行Python WSGI应用 Flask应用是基于WSGI规范的,所以...

python list多级排序知识点总结

在python3的sorted中去掉了cmp参数,转而推荐“key+lambda”的方式来排序。 如果需要对python的list进行多级排序。有如下的数据: list_num =...

Python cookbook(数据结构与算法)筛选及提取序列中元素的方法

本文实例讲述了Python筛选及提取序列中元素的方法。分享给大家供大家参考,具体如下: 问题:提取出序列中的值或者根据某些标准对序列做删减 解决方案:列表推导式、生成器表达式、使用内建的...