foreground
In the process of learning flask Web development, finally to the login registration interface, before using PIL to make their own verification code, now finally used, in the case of collecting various information. Finally completed the Flask Verification Code link Verification Code code
from PIL import Image, Imagefont, Imagedraw, imagefilter import random def validate_picture (): Total = ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345789 ' # picture size x Width = heighth = 50 # Mister into a new picture object im = Image.new (' RGB ', (width, heighth), ' White ') # Set font = Imagefont.truetype (' Frees Ans ', 40) # Create draw Object draw = Imagedraw.draw (im) str = ' # output each text for item in range (5): Text = Random.choice (total) str + = text Draw.text ((5+random.randint (4,7) +20*item,5+random.randint (3,7)), Text=te XT, Fill= ' black ', Font=font) # Draw a few interference lines for NUM in range (8): x1 = random.randint (0, width/2) y1 = r
Andom.randint (0, heighth/2) x2 = Random.randint (0, width) y2 = random.randint (HEIGHTH/2, heighth) Draw.line ((x1, y1), (x2,y2), fill= ' black ', width=1) # blur, add a handsome filter ~ im = Im.filter (imagefilter.find_edges) re Turn IM, str
I chose to write a verification code with a separate file that returns a string of Im objects and captcha forms.py
Class LoginForm (flaskform):
email = stringfield (' e-mail ', validators=[datarequired (), Length (1, +), email ()])
Password = Passwordfield (' Password ', validators=[datarequired ()])
Verify_code = Stringfield (' Captcha ', validators=[ DataRequired ()])
Remember_me = Booleanfield (' Remember your own user ')
submit = Submitfield (' login ')
views.py
@auth. Route ('/code ')
def get_code ():
image, str = validate_picture ()
# writes the captcha picture in binary form in memory, preventing the picture from being placed in the folder. Consume a large amount of disk
buf = Bytesio ()
image.save (buf, ' jpeg ')
buf_str = Buf.getvalue ()
# put the binary into response send back to the front end, and set the header field
response = Make_response (buf_str)
response.headers[' content-type '] = ' image/gif '
# Store the CAPTCHA string in session
session[' image ' = str
return response
@auth. Route ('/login ', methods=[' GET ', ' POST ')
def login ():
form = LoginForm ()
if Form.validate_on_ Submit ():
user = User.query.filter_by (email=form.email.data). First ()
if Session.get (' image ')! = Form.verify _code.data:
Flash (' captcha error ')
# Verify the user's login password if users are not
None and User.verify_password (form.password.data):
Login_user (User,form.remember_me.data)
Flash (' verification passed, login successful ')
return redirect (Request.args.get (' Next ') or
url_for (' Main.index '))
else:
Flash (' Username or password incorrect ')
return render_template (' auth/ Login.html ', Form=form)
login.html
{% extends "base.html"%}
{% import "bootstrap/wtf.html" as wtf%}
{% block title%} flasky-login{% Endblock%}
{% block page_content%}
<div class= "Page-header" >
Experimental Results
Login interface:
Response return the registration code image:
View a header message: Content-type to Image/gif
Again, a summary of the verification code will be refreshed after clicking
is a line of things to be, although looking from the Internet, and then combined with some of their own ideas. Also understood a little bit of response. A little bit about session and cookie, missing part: HTTP field information, and detailed understanding of session and Cookie. is doing this, although the daily free time is very little, but as much as possible to do something, one day in the web to achieve a function, use this write free time.