0

因此,我设计了一个在计算机上运行的程序,查找一直困扰我们的文件的特定方面,并在传递标志时删除文件。不幸的是,该程序似乎几乎随机关闭/崩溃。我说几乎是随机的,因为程序总是在删除文件后退出,尽管它通常会在成功后继续运行。

我运行了一个并行的 Python 程序,它以相同的时间间隔向上计数,但没有做任何其他事情。该程序不会崩溃/退出,并保持打开状态。

是否可能存在 R/W 访问问题?我以管理员身份运行该程序,所以我不确定为什么会这样。

这是代码:

import glob
import os
import time
import stat

#logging
import logging
logging.basicConfig(filename='disabledBots.log')
import datetime


runTimes = 0
currentPhp = 0
output = 0
output2 = 0
while runTimes >= 0:
    #Cycles through .php files
    openedProg = glob.glob('*.php')
    openedProg = openedProg[currentPhp:currentPhp+1]
    progInput = ''.join(openedProg)
    if progInput != '':
        theBot = open(progInput,'r')

        #Singles out "$output" on this particular line and closes the process
        readLines = theBot.readlines()
        wholeLine = (readLines[-4])
        output = wholeLine[4:11]

        #Singles out "set_time_limit(0)"
        wholeLine2 = (readLines[0])
        output2 = wholeLine2[6:23]

        theBot.close()
    if progInput == '':
        currentPhp = -1

    #Kills the program if it matches the code
    currentTime = datetime.datetime.now()
    if output == '$output':
        os.chmod(progInput, stat.S_IWRITE)
        os.remove(progInput)
        logging.warning(str(currentTime) +' ' + progInput + ' has been deleted. Please search for a faux httpd.exe process and kill it.')
        currentPhp = 0

    if output2 == 'set_time_limit(0)':
        os.chmod(progInput, stat.S_IWRITE)
        os.remove(progInput)
        logging.warning(str(currentTime) +' ' + progInput + ' has been deleted. Please search for a faux httpd.exe process and kill it.')
        currentPhp = 0
    else:
        currentPhp = currentPhp + 1
        time.sleep(30)

    #Prints the number of cycles    
    runTimes = runTimes + 1
    logging.warning((str(currentTime) + ' botKiller2.0 has scanned '+ str(runTimes) + ' times.'))
    print('botKiller3.0 has scanned ' + str(runTimes) + ' times.')
4

2 回答 2

1

首先,如果你的代码基于这样的东西,那么弄清楚发生了什么会容易得多......

for fname in glob.glob('*.php'):
    with open(fname) as fin:
        lines = fin.readlines()
    if '$output' in lines[-4] or 'set_time_limit(0)' in lines[0]:
        try:
            os.remove(fname)
        except IOError as e:
            print "Couldn't remove:", fname

错误,目前这实际上不是第二个,您现有的代码太棘手了,无法遵循句号,更不用说所有可能导致我们还不知道的奇怪错误的位!

于 2012-12-14T21:32:20.200 回答
0
if os.path.exists(progInput): 
    os.chmod(progInput, stat.S_IWRITE)
    os.remove(progInput)

还:

您从未在循环中重置 output 或 output2 变量?

这是故意的吗?

于 2012-12-14T21:26:32.933 回答