In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.