In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
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:
Summary
The above is the django introduced by the editor to you using pillow to simply set up the CAPTCHA function (python). I hope it will be helpful to you. If you have any questions, please leave a message for me, and the editor will reply to you in time. Thank you very much for your support to the website!
If you think this article is helpful to you, you are welcome to reprint it, please indicate the source, thank 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.