递归和多线程demo
Posted abc987plm
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了递归和多线程demo相关的知识,希望对你有一定的参考价值。
import zipfile import optparse, datetime, os from threading import Thread global i, ab i = 0 #记录测试次数 ab = ‘1234567890qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM‘ #密码字符组 def extractFile(zFile,password): try: zFile.extractall(pwd = password.encode(‘ascii‘)) #zFile.extractall的输入字符必须转码 print("[+] Found password " + password + " ") endtime = datetime.datetime.now() # print(‘end time:‘, endtime) print(‘spend time: %s 秒 完成校验密码,程序退出。‘ % ((endtime - starttime).seconds)) os._exit(0) except Exception as e: global i print(‘错误密码: %s‘ % password) i += 1 def can(contune=1, pd=‘‘): contune -= 1 for a in ab: if contune: can(contune, pd+a) #递归传参 password = pd + a t = Thread(target=extractFile, args=(zFile, password)) #创建多线程对象 t.start() t.join() def main(): global zFile, starttime parse = optparse.OptionParser("useage%prog " + "-f <zipfile> -d <passdwlen>") parse.add_option("-f",dest="zname",type="string",help="specify zip file") parse.add_option("-d",dest="passdwlen",type="int",help="specify password length") (options,args) = parse.parse_args() if (options.zname == None) | (options.passdwlen == None): print(parse.usage) else: starttime = datetime.datetime.now() # print(‘start time:%s‘ % starttime) zname = options.zname pdlen = options.passdwlen zFile = zipfile.ZipFile(zname) a = 1 while a<pdlen: can(a) a += 1 if __name__==‘__main__‘: main() endtime = datetime.datetime.now() # print(‘end time:‘, endtime) print(‘spend time: %s 秒 无正确密码,程序退出。‘ % ((endtime - starttime).seconds))
threading库的更多用法:
https://www.cnblogs.com/hiwuchong/p/8673183.html
optparse模块的更多用法:
https://www.cnblogs.com/darkpig/p/5677153.html
以上是关于递归和多线程demo的主要内容,如果未能解决你的问题,请参考以下文章