There are lot of spammers(send people alot messages) around. Sometimes bot trying to access your site to scrap data and fill false form. Many hackers use ddos attack on your site.In which there are lot of fake traffic sent to your site to pull its server down.
And there are many other problem faced. To which captcha provides a straightforward solution.
Here we will write a program to generate a random captcha list, using python inbuilt module
And on later part we will see how to solve captcha using python.
What is Captcha and how captcha is generated?
CAPTCHA stands for the Completely Automated Public Turing test to tell Computers and Humans Apart.
CAPTCHAs provide task like detecting bridges in image or recognising what letter is written on image which is difficult for robots and easier for humans.
It is used so that no machine or bots except human can access the site or service because all these attacks and scrapping things are done bots and automatic systems.
Demonstration of Captcha generator using python
How Captcha image is generated using programming?
Basic Captcha is generally generated from random string and converting it into image after some twisting, turning, tilting and coloring it.
In python we have random function to generate random text and numbers and we will use captcha module to generate captcha image. Lets code it.
Implementing Captcha generator and Checker using Python
Installing required module for Captacha generation
Generate barcode using Python Barcode Using Python barcode EAN13
Generating random string using random module
random.choices helps us to get random letters from ascii characters. ascii_uppercase makes picks only the letters which are in upper case and then we are concatenating them with digits using + symbol.
Generating Captcha using the random string
Using captcha we are creating a ImageCaptcha object. Which has a generate method in which we are passing a random string to generate a captcha image that will be stored in data variable.
After using write method we are creating a ‘CAPTCHA.png’ file on our folder.
You can open that Captcha png file and see that it has that random string on it.
Creating a Captcha Checker using Python
We will input string from user and compare that string from actual random_String.
If both are equal or same then you recognised captcha correctly and you must be a human. Bot will not be able to recognize it in most of the cases.
Using simple if conditional we are checking whether the string you passed and randomly generated string were same?
If yes then we are moving on further step like submitting form or referring to some other webpages. Else we are restricting the user from accessing the service.
Hope you find it interesting and will implement this on your system. Let me know in the commments did this worked on yours? And how can I help you. Suggest me some ways to improve the quality on my web pages and content.