Javatpoint Logo
Javatpoint Logo

Program to Generate CAPTCHA and Verify user using Python

Introduction:

This tutorial teaches us about a program to generate CAPTCHA and verify users using Python. The CAPTCHA is used in various websites to check whether the user is a human or a robot. It is mainly used for security purposes. For using CAPTCHA, except for humans, no one can enter the website. Its main goal is to develop practical solutions to experimental problems that are easy for humans but difficult for robots or computers. They test the cognitive ability of the users.

In other words, the task is to create a unique CAPTCHA for each time and check whether the user is human. The user needs to enter the Captcha that was automatically generated and examine the generated Captcha with the user input.

The character set for generating the CAPTCHA code is stored in the chrs[] character array. char[] is containing (a-z, A-Z, 0-9), so the size of chrs[] is 62.

A random number is generated in Python using the rand() function. It can be written as rand()%62. The rand()%62 can generate random CAPTCHA between 0 and 61. This function is used to generate a unique CAPTCHA every time. Generated a random number as an index to the character array chrs[], thus creating a new captcha character. This loop runs for n time to generate a captcha of the given length. Here, n represents a captcha length.

Example:

Here, we give two examples of the input and output of CAPTCHA.

What are the types of CAPTCHAS?

There are various types of CAPTCHAS are used, which are -

  1. Text based CAPTCHA
  2. Image based CAPTCHA
  3. Audio based CAPTCHA
  4. Behavioural based CAPTCHA

Algorithm:

Here, we learn about the algorithm of how to generate CAPTCHA and verify users using Python.

  1. Firstly, import a random library.
  2. Then, we define the CaptchaCheck function with two parameters: Captcha and user_captcha. These parameters are return Boolean value.
  3. In the CaptchaCheck function, we will check whether the two parameters are equal. If two parameters are equal, it returns true; otherwise, it returns false.
  4. Then, we define the CaptchaGenerator function to generate a n length of CAPTCHA.
  5. With this function, define a character array with all the values (a-z, A-Z, 0-9), which will used to generate a Captcha.
  6. Generate n length of random Captcha.
  7. In the main() function, declare a different string named Captcha and call CaptchaGenerator() with length 10 to generate a Random CAPTCHA string.
  8. Then, the user needs to give input according to the given Captcha.
  9. If the user input equals the given Captcha, then the CapchaCheck function returns a message that the Captcha is matched. If it is not equal, then this function returns a message that the Captcha is not matched.

Program Code:

Here we give a program code that will generate CAPTCHA and verify the user using a Python program. The code is now given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

CAPTCHA is: hUWjrhv4n1
Enter the given CAPTCHA: hUWjrhv4n1
The CAPTCHA is Matched here

If we put the wrong Captcha, then the output is -

CAPTCHA is: BFIVDLAca8
Enter the given CAPTCHA: BFIVDLAc
The CAPTCHA is not Matched here

Conclusion:

So, in this tutorial, we are learning about a program to generate CAPTCHA and verify users using Python. Creating a captcha is a great way to prevent suspicious people from accessing your website. CAPTCHA is used in various websites to check whether the user is a human or a robot. Every website needs captchas because it's riskier than ever in today's modern technology world.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news