Python Captcha Verification Example
Published February 07, 2022A capture is a form of automated challenge that is used to determine if the user of an application is a human or robot. It makes it exceedingly simple for humans to execute a challenge that is difficult for computers or robots to complete. This challenge may be completed by solving basic math problems, identifying stretched numerals or characters, matching patterns, clicking on certain items, and so forth. In this article, we'll look at a Python example of a Captcha verification program.
How to Make a Python Captcha Program?
We are going to create a python captcha verification program using the Python Tkinter. We are going to use a random module that will make the choice of numbers and alphabets randomly and join them into a string.
The easy steps for creating a Python captcha verification program are outlined below.
Step 1: First of all, import the Tkinter library in the application using the code below
from tkinter import * from tkinter import messagebox import random |
Step 2: Now, paste the code below to build the GUI application. In our application GUI, we are going to display the randomly generated captcha, an input text field for the users to enter capture, and the button to verify the entered capture.
from tkinter import * from tkinter import messagebox import random text = 'abcdefghijklmnopqrstuvwxyz0123456789' root = Tk() root.title("Python Captcha Verification Example") root.geometry("400x200") captcha = StringVar() input = StringVar() Label(root, textvariable=captcha, font="ariel 16 bold").pack(padx=5, pady=5) Entry(root, textvariable=input, bg='white', font="ariel 12 bold").pack(padx=5, pady=5) Button(root, text="verify", font="ariel 15 bold", bg='gray', command=check).pack(padx=5, pady=5) root.mainloop() |
If you run the above code, the following application will be displayed.
![]() |
Step 3: Now create functions to generate and verify the generated captcha.
Function to generate Captcha of 6 Characters
def create_captcha(): c = random.choices(text, k = 6) captcha.set(''.join(c)) Function to Verify the user entered Captcha def check(): if captcha.get() == input.get(): messagebox.showinfo('Results', 'Captcha Verification Success..') else: messagebox.showerror('Results', 'Captcha Verification failed') input.set('') create_captcha() |
This function will either display “Captcha verification success” or “captcha verification failed”.
Step 4: Now, run the application and test whether everything works as expected.
Complete code for Image Captcha verification in python
from tkinter import * from tkinter import messagebox import random text = 'abcdefghijklmnopqrstuvwxyz0123456789' root = Tk() root.title("Python Captcha Verification Example") root.geometry("400x200") captcha = StringVar() input = StringVar() def create_captcha(): c = random.choices(text, k = 6) captcha.set(''.join(c)) def check(): if captcha.get() == input.get(): messagebox.showinfo('Results', 'Captcha Verification Success..') else: messagebox.showerror('Results', 'Captcha Verification failed') input.set('') create_captcha() Label(root, textvariable=captcha, font="ariel 16 bold").pack(padx=5, pady=5) Entry(root, textvariable=input, bg='white', font="ariel 12 bold").pack(padx=5, pady=5) Button(root, text="verify", font="ariel 15 bold", bg='gray', command=check).pack(padx=5, pady=5) create_captcha() root.mainloop() |
Output
![]() |
![]() |
![]() |
Conclusion: In this python example we implemented Captcha verification
Article Contributed By :
|
|
|
|
270 Views |