gpt4 book ai didi

Python 2.7 从 main 退出并返回退出状态,我做错了什么?

转载 作者:行者123 更新时间:2023-12-01 05:15:26 24 4
gpt4 key购买 nike

这是一个无限循环问题,评论中的 friend 纠正了我的观点,如果您愿意,请随时将其删除。

我在这里阅读了一些其他问题和答案,并尝试实现这些建议,但无济于事。因为我不是在编写专有代码,所以我可以完整地发布此内容,它不会那么长,但我希望堆栈交换不介意...另外,请随意使用和/或根据您的意愿进行修改。

    #!/usr/bin/env python2.7

import sys, random, subprocess, signal


def main(argv=None):
if argv is None:
argv = sys.argv
def signal_handler(signal, frame):
fdHT.close()
fdWL.close()
print '\n'
return 1

signal.signal(signal.SIGINT, signal_handler)

pathToWL = str(sys.argv[1])
pathForHT = str(sys.argv[2])
mId = str(sys.argv[3])

SaltCharSet = str("a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9")
SaltCharSet = SaltCharSet.split(" ")

try:
fdWL = open(pathToWL, 'r')
except:
print "Could not open wordlist file."
return 2

try:
fdHT = open(pathForHT, 'a')
except:
print "Could not open file for hash table"
fdWL.close()
return 3

#We have our wordlist now step through the thing and start generating hashes.

toStop = False
#cursor = 0 #Use the cursor later once this program evolves

#print 'Entering while 1'
while(toStop == False):
try:
ln = str(fdWL.readline())
except:
fdHT.close()
fdWL.close()
return 4
if ln == '':
toStop = True #This should have been an ASSIGNMENT not a test, was ==
ln = ln.strip("\n")
ln = ln.strip("\r")
if len(ln) < 6:
continue
# create random salts
# send ln, id, and salts to hashpipe
salt = []
lenOfSalt = random.randint(6,16)
#print 'Entering while 2'
while(len(salt) < lenOfSalt + 1):
aORn = random.randint(0,1)
if aORn == 0:# Its a letter
uORl = random.randint(0,1)
if uORl == 0:
salt.append(SaltCharSet[(random.randint(0,25))].upper())
elif uORl == 1:
salt.append(SaltCharSet[(random.randint(0,25))].lower())
else:
print "Random Int 'uORl' out of bounds"
fdHT.close()
fdWL.close()
toStop = True
return 5 # I don't know what happened
elif aORn == 1:# Its a number
salt.append(SaltCharSet[(random.randint(26, 35))])
else:
print "Random Int 'aORn' out of bounds"
fdHT.close()
fdWL.close()
toStop = True
return 6 # I don't know what happened
#Generated Salt
salt = "".join(salt)
wholeArg2 = str("$"+mId+"$"+salt)
try:
mHash = str(subprocess.check_output([r"hashpipe", ln, wholeArg2]))
except:
print " error getting hash"
#Clean-up
fdHT.close()
fdWL.close()
toStop = True
return 7
#Generated hash, now write it to the fdHT file
print str(ln+"\t"+mHash)
fdHT.write(str(ln+"\t"+mHash))
#cursor = fdWL.tell()

print 'done with whiles'
fdHT.close()
fdWL.close()

return 0

if __name__ == "__main__":
sys.exit(main())

这个脚本调用了我编写的一个小 C 程序,使用 GNU crypt_r() 函数对字符串进行哈希处理...它卡在最后,我必须使用 ctrl-c 来保释...这是一些示例输出。 ..

    zurumba`tico    $6$96u6sUy05rM69$1NLxLYXS9tAf05szgV0/GH6pvykOPsuEIlGxOkDOMNEixGiN8oeTG.xxIq/D19YpArMWtD1xJMG9sKWgA9xzK/

zurupeto $6$O2510Y900o02008$BO2OadT8Bvje78C2JhuZ6r/.iJHz.s9UfET8MU93iGy57bbe/qh9/Uj4jSkVSCyknegnkAB2JF7vRgWohkGVI0

zutana $6$Ur2i9m95$E2WqrEnld4aPa1bYAlCNnMEE0nmwxNlfB9ozVc3I6NCKXHqnSyspZrqIq2usqNf2JwlVF1myhqIn26a71Dm510

zutano $6$8x482Lar4qj$LupCZ9t2ImG.nRVH9xHPsyyx9emiImNTRaErxNrtsGyWjeO3XZLzj.F1D3eQOsiurQeQMWeQ3lF5ef.o3iesg.

zuzo`n $6$G9JP2GE$FAoZGGQNNycPfGCHq/Ra4MSQNknATMgHLzk8N9FHDefbZm/Hcx6JdV/sZdbkFHVVkoRjTnoUP9mw6CkE/.4fI.

^C

我哪里出错了?

最佳答案

我认为您可以进行两项更改,这将提高您编写正确工作的代码的机会。

首先,使用 with 语句来处理文件的打开和关闭。如果代码中稍后发生异常或 return 语句,则使用 with open("filename") as file_obj 将自动关闭文件。

其次,使用 for 循环读取文件行,而不是在 while 循环的每个周期调用 readlinefor 循环将运行直到找到文件末尾,然后停止。如果您需要对文件结尾做出与其他终止条件不同的 react ,则可以对特殊终止情况使用 break 并在循环上放置一个 else 子句(这仅当 for 循环完成且没有中断时才会运行)。

关于Python 2.7 从 main 退出并返回退出状态,我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23328438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com