Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to set the CAPTCHA function of python

Shulou Source: shulou.com Published: 2022-05-31 11:22:06 10月12日 Update

This article introduces the knowledge of "how to set the CAPTCHA function in python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Import the module and define a validation status

From pil import image, imagedraw, imagefontfrom django.utils.six import bytesiodef verify_code (request): # introduce random function module import random # to define variables Background color, width, height bgcolor = (random.randrange (20,100), random.randrange (20,100), 255) width = 100height = 25 # create picture object im = image.new ('rgb', (width, height), bgcolor) # create brush object draw = imagedraw.draw (im) # call point () function of the brush to draw noise for i in range (0100): xy = (random.randrange (0, width)) Random.randrange (0, height)) fill= (random.randrange (0255), 255,255, random.randrange (0255)) draw.point (xy, fill=fill) # defines the optional value of the CAPTCHA str1 = 'abcd123efghijk456lmnopqrs789tuvwxyz0' # randomly selects four values as the CAPTCHA rand_str =' 'for i in range (0,4): rand_str + = str1 [random.randrange (0, len (str1))] # to construct the font object The font path of ubuntu is "/ usr/share/fonts/truetype/freefont" font= imagefont.truetype ('freemono.ttf', 23) # construct font color fontcolor = (255, random.randrange (0,255), random.randrange (0255)) # draw four words draw.text ((5,2), rand_str [0], font=font, fill=fontcolor) draw.text ((25,2), rand_str [1], font=font, fill=fontcolor) draw.text ((50,2) Rand_str [2], font=font, fill=fontcolor) draw.text ((75,2), rand_str [3], font=font, fill=fontcolor) # release brush del draw # save to session Used for further verification request.session ['verifycode'] = rand_str # memory file operation buf = bytesio () # Save the picture in memory, the file type is png im.save (buf,' png') # return the image data in memory to the client, and the mime type is picture png return httpresponse (buf.getvalue (), 'image/png')

3. Put it directly into the img in the web page

验证码

4. Use ajax to obtain authentication password and account number

Login

Please enter a verification code

验证码

Remember the password

Click to get cookies $(function () {$('# ajax') .click (function () {username = $('# username'). Val (); password = $('# password'). Val (); ow = $("# ow"). Val (); yum = $('# yum'). Val () $.ajax ({'url':' / loginajax', 'type':' post', 'data': {' username': username, 'password': password, "yum": yum,} 'success':function (data) {/ / successful login returns 1 / / login failure returns 0 / / Verification failure returns 3 if (data.res = = 1) {$('.show') .show () .html ('login successful')} else if (data.res = = 0) { $('.show') .show () .html ('login failed')} else if (data.res = = 3) {$('.show') .show () .html ('CAPTCHA input failed') );})

The account password and verification code are sent to the server in the ajax above.

In the validation function

Yzm = request.post.get ('yum') # get the incoming CAPTCHA vaue = request.session [' verifycode'] # save the CAPTCHA in session when generating the picture if yzm! = vaue: # if it is not equal, 3 html ajax will be returned and verification error return jsonresponse ({'res':3}) will be displayed.

Achievement map:

This is the end of the content of "how to set up the CAPTCHA function in python". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Validation picture login memory function font password object brush function success content file more module screen knowledge type account number input Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Redmi MariaDB Shulou Tech Info NVidia