码迷,mamicode.com
首页 > 编程语言 > 详细

递归和多线程demo

时间:2019-11-26 19:53:05      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:except   lse   use   int   sea   add   blog   ons   对象   

 

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

递归和多线程demo

标签:except   lse   use   int   sea   add   blog   ons   对象   

原文地址:https://www.cnblogs.com/abc987plm/p/11937456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!