遞歸和多線程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 + "\n")
        endtime = datetime.datetime.now()
        # print('end time:', endtime)
        print('spend time: %s 秒 \n\n完成校驗密碼,程序退出。' % ((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 秒 \n\n無正確密碼,程序退出。' % ((endtime - starttime).seconds))

threading庫的更多用法:

https://www.cnblogs.com/hiwuchong/p/8673183.html

optparse模塊的更多用法:

https://www.cnblogs.com/darkpig/p/5677153.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM