首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有‘`argparse`’的AttributeError

带有‘`argparse`’的AttributeError
EN

Stack Overflow用户
提问于 2019-01-30 00:26:19
回答 1查看 341关注 0票数 0

所以我正在重写一个ZIP破解器,它是由TJ‘’Connor在Python2.7上发布的。作者使用了optparse,但我和argparse一起去了。

我的代码如下:

代码语言:javascript
复制
import argparse
from threading import Thread
import zipfile
import io

parser = argparse.ArgumentParser(description="Unzips selected .zip using a dictionary attack", usage="CRARk.py -z zipname.zip -f file.txt")

# Creates -z arg
parser.add_argument("-z", "--zip", metavar="", required=True, help="Location and the name of the .zip file.")

# Creates -f arg
parser.add_argument("-f", "--file", metavar="", required=True, help="Location and the name of the word-list/dictionary-list/password-list.")
args = parser.parse_args()


def extract_zip(zipFile, password):
    try:
        zipFile.extractall(pwd=password.encode())
        print("[+] Password for the .zip: {0}".format(password) + "\n")
    except:
        pass


def main(zip, dictionary):
    if (zip == None) | (dictionary == None):
        print(parser.usage)
        exit(0)
    zip_file = zipfile.ZipFile(zip)
    pass_file = io.open(dictionary, mode="r", encoding="utf-8")
    for line in pass_file.readlines():
        password = line.strip("\n")
        t = Thread(target=extract_zip, args=(zip_file, password))
        t.start()


if __name__ == '__main__':
    # USAGE - Project.py -z zipname.zip -f file.txt
    main(args.zip, args.dictionary)

我所犯的错误是:

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:\Users\User\Documents\Jetbrains\PyCharm\Project\Project.py", line 39, in <module>
    main(args.zip, args.dictionary)
AttributeError: 'Namespace' object has no attribute 'dictionary'

现在我有点不确定那是什么意思。我尝试将args.dictionary重命名为args.file或类似的,但是当我运行代码时,它最终在我的终端上返回一个空响应。如下图所示,当我正确运行.py时,没有响应/输出等。

我怎么才能解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2019-01-30 01:27:49

使用代码中的part解析部分:

代码语言:javascript
复制
import argparse
parser = argparse.ArgumentParser(description="Unzips selected .zip using a dictionary attack", usage="CRARk.py -z zipname.zip -f file.txt")

# Creates -z arg
parser.add_argument("-z", "--zip", metavar="", required=True, help="Location and the name of the .zip file.")

# Creates -f arg
parser.add_argument("-f", "--file", metavar="", required=True, help="Location and the name of the word-list/dictionary-list/password-list.")
args = parser.parse_args()
print(args)

样本运行:

代码语言:javascript
复制
2033:~/mypy$ python3 stack54431649.py -h
usage: CRARk.py -z zipname.zip -f file.txt

Unzips selected .zip using a dictionary attack

optional arguments:
  -h, --help    show this help message and exit
  -z , --zip    Location and the name of the .zip file.
  -f , --file   Location and the name of the word-list/dictionary-
                list/password-list.
1726:~/mypy$ python3 stack54431649.py
usage: CRARk.py -z zipname.zip -f file.txt
stack54431649.py: error: the following arguments are required: -z/--zip, -f/--file
1726:~/mypy$ python3 stack54431649.py -z zippy -f afile
Namespace(file='afile', zip='zippy')

这意味着我可以使用

代码语言:javascript
复制
args.file    # 'afile'
args.zip     # 'zippy'
main(args.zip, args.file)

main

代码语言:javascript
复制
if zip is None:    # better than zip == None

但是,由于这两个参数是必需的,所以它们永远不会是None

接下来的问题是,zip的值是否是有效zip文件的名称,以及是否可以打开dictionary

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54431649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档