Python verification code based on Django, djangopython
Verification Code
- On the user registration and logon page, you can add the verification code function to prevent brute-force requests. If the verification code is incorrect, you do not need to proceed with the verification code. This reduces the pressure on some servers.
- Verification Code is also an effective method to prevent crsf
- The verification code is as follows:
Verification Code view
From django. http import HttpResponsedef verifycode (request): # introduce the drawing module from PIL import Image, ImageDraw, ImageFont # introduce the random function module import random # define the variable, bgcolor = (random. randrange (20,100), random. randrange (20,100), 255) width = 100 height = 25 # Create Image Object im = Image. new ('rgb ', (width, height), bgcolor) # create the paint brush object draw = ImageDraw. draw (im) # Call the point () function of the paint brush to Draw noise for I in range (0,100): xy = (random. randrange (0, width), random. randrange (0, height) fill = (random. randrange (0,255), 255, random. randrange (0,255) draw. point (xy, fill = fill) # define the Alternative value of the Verification Code str1 = 'abc123efghijk456lmnopqrs789tuvwxyz0' # randomly select four values as the verification code rand_str = ''for I in range (0, 4): rand_str + = str1 [random. randrange (0, len (str1)] # construct the font object font = ImageFont. truetype ('freemono. ttf', 23) # construct fontcolor = (255, random. randrange (0,255), random. randrange (0,255) # draw 4 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 the paint brush del draw # Save it to the session for further verification request. session ['verifycode'] = rand_str # memory file operation import io buf = io. stringIO () # Save the image in the memory. The file type is png im. save (buf, 'png ') # return the image data in the memory to the client. The MIME type is png return HttpResponse (buf. getvalue (), 'image/png ')
Configure url
- Define the url of the request verification code view in urls. py
from . import viewsUtilurlpatterns = [ url(r'^verifycode/$', viewsUtil.verifycode),]
Show Verification Code
- Use the img label in the template, and src points to the Verification Code view.
- Start the server. Check that the server is successfully started.
- Extension: When you click "cannot see clearly, change one", you can change a new verification code.
<Script type = "text/javascript" src = "/static/jquery-1.12.4.min.js"> </script> <script type = "text/javascript"> $ (function () {comment ('{verifycodechange'}.css ('cursor ', 'pointer '). click (function () {$ ('# verifycode '). attr ('src', $ ('# verifycode '). attr ('src') + 1)}) ;}); </script> <span id = 'verifycodechang'> can't see clearly, change one </span>
- To implement the submission function, you need to add the form and input labels.
<Form method = 'post' action = '/verifycodeValid/'> <input type = "text" name = "vc"> <span id = 'verifycodechang'> cannot see clearly, change one </span> <br> <input type = "submit" value = "submit"> </form>
Verify
- Received request information, compared with the content in the session
from django.http import HttpResponsedef verifycodeValid(request): vc = request.POST['vc'] if vc.upper() == request.session['verifycode']: return HttpResponse('ok') else: return HttpResponse('no')
- Configure the authentication url
urlpatterns = [ url(r'^verifycodeValid/$', views.verifycodeValid),]