Today’s topic of discussion is OTP or One-Time-Password or more commonly what we know as OTP verification using Python. Let us see how we can utilize the multitude of Python libraries at our disposal to verify OTP.

OTP Verification Python

Have you ever pondered over why and how we get a different One-Time_Password (for verification and authentication); each time we transact online or use a Payment Gateway? I used to think over it and got to the realization that each entity; has its own way of building and implementing OTP; for authentication purposes. But, generally, this is a randomly generated 4-digit or a 6-digit code. Hence, with that thought, I will make you learn to do OTP verification using Python yourself. Thus, by the end of it all, you will be able to verify any id for OTP.

Before we move on, here are some tips for you:

  • OTP is a One-Time-Password, that is a unique code sent to your ID to verify you as an authentic user; and thereby, complete the online transaction.
  • We avail OTP generally doing online transactions or sometimes to verify your devices; in case you forget your password or cases like that.
  • Thus, OTP is generally used to authenticate you as a valid user and complete the task that you are doing.
OTP Verification Python

Now, it’s quite simple to create a program to do this task of OTP verification using Python. Here are the main points we need to take care of in general cases :

  • We need to build a 4-digit or a 6-digit random number and store it in memory(in a variable).
  • Then, write one program to send email
  • We need to use this OTP as the message there.
  • Thereafter, request the user for the two inputs namely: the user’s email ID; and thereafter, the One-Time_Password receiver by the user.
  • Hurray! you have successfully verified your OTP thereby validating your authenticity as a valid (legal) user.

Code Analysis for OTP verification using Python

So, now that we know the various steps we need to follow; let’s get to the coding part to fully acquaint ourselves with this program.

  • Firstly, we need to import some libraries necessary for this task :
import os                # This is used for system function
import math          # The math library
import random     # For random numbers
import smtplib      # For email functions
  • Next, as discussed above we need to generated random numbers and store it in memory in a variable. So, that we can utilize the same when sending a verification email.
num = "0123456789"
val = ""
for i in range(0,6):
    val+ = num[math.floor(random.random()*10)]
OTP = val + " is your One-Time-Password for verification"
message = OTP
  • Now, moving further, we will require your account password(to send emails); here we will take the Gmail account as an example.
  • Thereafter, after this app password is created, a key will get generated.
  • We will require to copy it and paste in the code below to send an email and verify the OTP.
email = smtplib.SMTP('smtp.gmail.com', 587)  # To call the gmail account client
email.starttls()
email.login("Your Google Account", "You application password")  # To login into your account successfully
id = input("Enter your email address : ")
email.sendmail('&&&&&&&&&&&', emailid, message)   # Sending the OTP email
x = input("Enter your One-Time-Password ~~> :  ")
if x == val :
    print("Hurray!! Your account has been successfully verified !! ")
else:
    print("Please carefully check your OTP once again !! ")

Here’s a custom output generated while validating my email using the OTP.

Enter your email address : pcXXXXXXX@gmail.com
Enter your One-Time-Password ~~> : 780564
Hurray!! Your account has been successfully verified !!

Want to learn how to keep asking the user for input until they give a valid response Python ??

Full Code for OTP verification using Python

Now, that we are aware of this method; let’s take a look at the full code snippet below, to make it further clear :

import os                # This is used for system function
import math          # The math library
import random     # For random numbers
import smtplib      # For email functions

num = "0123456789"
val = ""
for i in range(0,6):
    val+ = num[math.floor(random.random()*10)]
OTP = val + " is your One-Time-Password for verification"
message = OTP

email = smtplib.SMTP('smtp.gmail.com', 587)  # To call the gmail account client
email.starttls()
email.login("Your Google Account", "You application password")  # To login into your account successfully
id = input("Enter your email address : ")
email.sendmail('&&&&&&&&&&&', emailid, message)  # Sending the OTP email
x = input("Enter your One-Time-Password ~~> :  ")
if x == val :
    print("Hurray!! Your account has been successfully verified !! ")
else:
    print("Please carefully check your OTP once again !! ")
OTP Verification Python

Miscellaneous : Python code to create alpha-numeric OTP

import math
import random
 
# defining a function to create the alpha-numeric One-Time-Password
def OTP() :
     # declaring the variable to store alpha-numeric characters
    sp = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    val = ""
    length = len(sp)
    for i in range(0,6) :
        val+ = sp[math.floor(random.random() * length)]
    return val
 
# Driver code
if __name__ == "__main__" :
     print("Your One-Time-Password is ~~> ", OTP())

Output :

Your One-Time-Password is ~~> aX23v4

WRAPPING UP !!

In this quick guide, you learn about what an OTP is? Also, how do various entities bring the One-Time-Password in use; to authenticate and validate your identity and thereby, identify you as a valid and legal user? We also see the OTP verification program using Python coding. Hoping that you take some good quality knowledge back with you. On that note, until next time, see ya!! Goodbye !! :)~~

** :::::: ** ::))::((~~>>

Categorized in: