- Home
- Course Detail
regularpython@gmail.com
You are now watching:
Python Random Numbers / of Python Random Numbers Real-Time Examples
random_realtime_example.py
import random import MySQLdb from python_random_numbers.mail import otpMail try: connection = MySQLdb.connect(user='root',password='sairam143sairam',port=3306,host='localhost') homeCursor = connection.cursor() except Exception as e: print(e) else: name = input("Please enter your username.") password = input("Please enter your password.") try: homeCursor.execute(f"SELECT gmail FROM credentials.users where username='{name}' and password='{password}';") mail = homeCursor.fetchone() if mail is not None: print("Your credentials are successfully verified.\nYou have to do one more step to login into your account.\n"+ "we send opt to your mail please enter it here.") otp = random.randint(100000,999999) #send otp to the user gmail account try: otpMail().mail(mail[0], otp) except: print("Error in sending otp") otp2 = input("Please enter your otp here") try: otp2 = int(otp2) except: print("please enter correct otp.") if otp == otp2: print("You have successfully logged in.") else: print("login failed") else: print("Please Enter Correct Credentials") except Exception as e: print(e)
mail.py
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from python_random_numbers.password import password class otpMail: def mail(self,receiver,otp): fromaddr = "sairam.doddaparthi1992@gmail.com" toaddr = receiver msg = MIMEMultipart() msg['From'] = fromaddr msg['To'] = toaddr msg['Subject'] ="One TIme Password" message=f''' hi, Thank you for choosing my video tutorials. I hope you get better understanding on python. This is your one time password.Please enter it to login into your account OPT : {otp} ''' # body = "hi sairam how are you?" msg.as_string("hi") msg.add_header('Content-Type', 'text') msg.set_payload(message) server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(fromaddr, password) text = msg.as_string() server.sendmail(fromaddr, toaddr, text) server.quit()
password.py
password = "your password here"