我正在尝试将avg作为程序的一部分运行。该程序通常是自动执行的,所以我看不到python的标准输出。
当我直接调用程序来运行程序时,它可以正常运行,但是当我通过自动化程序运行时,它会失败。
它将在系统日志中显示->“开始扫描:xxx”,但从不显示“意外错误”或“扫描结果”。这意味着,它失败了,但是没有使用catch语句,或者在“ out”变量中报告了错误。
令人反感的功能:
# Scan File for viruses
# fpath -> fullpath, tname -> filename, tpath -> path to file
def scan(fpath, tname, tpath):
syslog("Starting scan of: " + tname)
command = ["avgscan",
"--report=" + tpath + "scan_result-" + tname +".txt",
fpath]
try:
out = subprocess.call(command)
syslog("Scan Results: " + str(out))
except:
syslog("Unexpected error: " + sys.exc_info()[0])
finally:
syslog("Finished scan()")
到目前为止,这两个想法都是围绕调试代码本身进行的,在此之前,扫描只是一个简单的subprocess.call(command)和简单的syslog输出。在with语句中,添加了try catch以帮助调试。
所以我解决了。解决了我不再使用AVG Scan和libclamscan的问题。
通过使用直接与python一起使用的扫描仪,结果更快,错误也消失了。如果有人通过搜索遇到了这个,这是我现在正在使用的代码:
import os.path
import pyclamav.scanfile
def r_scan(fpath):
viruslist = []
if os.path.isfile(fpath):
viruslist = f_scan(fpath, viruslist)
for root, subFolders, files in os.walk(fpath):
for filename in files:
viruslist = f_scan(
os.path.join(root, filename), viruslist)
writeReport(fpath, viruslist)
def f_scan(filename, viruslist):
result = pyclamav.scanfile(filename)
if result[0] > 0:
viruslist.append([result[1], filename])
return viruslist
def writeReport(fpath, viruslist):
header = "Scan Results: \n"
body = ""
for virusname, filename in viruslist:
body = body + "\nVirus Found: " + virusname + " : " + filename
with open(fpath + "-SCAN_RESULTS.txt", 'w') as f:
f.write(header+body)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句