| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 
 | def verifycode(request):
 from PIL import Image, ImageDraw, ImageFont
 
 import random
 
 bgcolor = (random.randrange(20, 100), random.randrange(
 20, 100), random.randrange(20, 100))
 width = 100
 height = 50
 
 im = Image.new('RGB', (width, height), bgcolor)
 
 draw = ImageDraw.Draw(im)
 
 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)
 
 str = '1234567890QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm'
 
 rand_str = ''
 for i in range(0, 4):
 rand_str += str[random.randrange(0, len(str))]
 
 font = ImageFont.truetype(r'C:\Windows\Fonts\Arial.ttf', 40)
 
 fontcolor1 = (255, random.randrange(0, 255), random.randrange(0, 255))
 fontcolor2 = (255, random.randrange(0, 255), random.randrange(0, 255))
 fontcolor3 = (255, random.randrange(0, 255), random.randrange(0, 255))
 fontcolor4 = (255, random.randrange(0, 255), random.randrange(0, 255))
 
 draw.text((5, 2), rand_str[0], font=font, fill=fontcolor1)
 draw.text((25, 2), rand_str[1], font=font, fill=fontcolor2)
 draw.text((50, 2), rand_str[2], font=font, fill=fontcolor3)
 draw.text((75, 2), rand_str[3], font=font, fill=fontcolor4)
 
 del draw
 
 import io
 buf = io.BytesIO()
 
 im.save(buf, 'png')
 
 response = HttpResponse(buf.getvalue(), 'image/png')
 
 
 request.session['verifycode'] = rand_str
 
 
 
 
 return response
 
 
 def login(request):
 if request.method == "GET":
 return render(request, 'myApp/login.html')
 else:
 verifycode = request.POST.get("verifycode")
 
 if verifycode.upper() == request.session.get("verifycode").upper():
 
 
 if request.POST.get("username") == "sunck" and request.POST.get("passwd") == "123qweasdzxc":
 
 return redirect("/")
 else:
 
 return redirect("/login/")
 else:
 
 return redirect("/login/")
 
 |