one, individually encapsulating a tool class to generate a picture verification code
Import Random # random number import string # character # The package from PIL import image,imagedraw,imagefont,imagefile that introduces the drawing introduces the cached package from Django.
Core.cache Import Cache # Defines the class class Captcha (object) of the authentication code: # The path of the selected font font_path = ' Verdana.ttf ' # generates several number of CAPTCHA codes Number = 4 # generate Captcha picture width and height size = (100,30) # background color, default is white bgcolor = (255,255,255) # font color, default to blue Fon TColor = (random.randint (0), Random.randint (0), Random.randint (0, 100)) # Verify code font size fontsize = 25 # interference The color of the line, the default is red LineColor = (random.randint (0, 255), Random.randint (0, 255), Random.randint (0, 255)) # Do you want to add a distractors to the line DRA W_line = true # whether to draw the jamming point draw_point = true # Add the number of lines Line_number = 2 # define a class method # to randomly generate a string (including English
and numbers) @classmethod def __gene_text (CLS): Source = List (string.letters) for index in range (0,10): Source.append (str (index)) # Randomly acquire several digital concatenation return '. Join (Random.sample (Source,cls.number)) # Set A semantic class method to draw a disturbing line @classmeThod def __gene_line (cls,draw,width,height): # start position begin = (Random.randint (0,width), Random.randint (0, Height) # ending position end = (Random.randint (0,width), Random.randint (0,height)) draw.line ([begin, end], F
Ill=cls.linecolor) # defines a class method to draw the jamming point @classmethod def __gene_points (cls,draw,point_chance,width,height): Chance = min (0, int (point_chance))) # size is limited to [0] for W-xrange (width): for h in xrange (height): tmp = random.randint (0) if tmp > 100-chance:draw. Point ((W, h), fill= (0, 0, 0)) # Generate a CAPTCHA @classmethod def gene_code (CLS): width, height = cls.size # width High image = Image.new (' RGBA ', (width, height), cls.bgcolor) # Create picture font = Imagefont.truetype (Cls.font_path, Cls.fontsize) # verification Code Font draw = Imagedraw.draw (image) # Create a brush text = Cls.__gene_text () # Generate string fo Nt_width, font_height =Font.getsize (text) Draw.text ((width-font_width)/2, (Height-font_height)/2), text, Font=font, Fill=cls.fontcolor) # Fill String # If you need to draw a jamming line if Cls.draw_line: # traverse the Line_number times, is the painting line_numb ER root line for x in xrange (0, Cls.line_number): Cls.__gene_line (Draw, width, height) # if necessary Draw noise if cls.draw_point:cls.__gene_points (draw, width, height) # saved to cache, expires in 2 minutes C Ache.set (Text.lower (), Text.lower (), Check_captcha) return (text, image) # Verify the code @classmethod def the
(CLS, captcha): Captcha_cache = Cache.get (CAPTCHA) if captcha_cache and Captcha_cache = = Captcha: # Delete Cache Cache.delete (CAPTCHA) return True Else:return False
Ii. Configuring the cache used in settings.py
# Configure local Cache
caches = {'
default ': {
' backend ': ' Django.core.cache.backends.memcached.MemcachedCache ',
' LOCATION ': [
' 127.0.0.1:11211 ',
]
}
}
Third, open the local memcached cache
1, if not installed
memcached-d Install
2, start
memcached-d start