<span style="color: #0000ff;">from
flask <span style="color: #0000ff;">import<span style="color: #000000;"> Flask,render_template,url_for,request,redirect,make_response,session
<span style="color: #0000ff;">import<span style="color: #000000;"> os,MySQLdb
app = Flask(<span style="color: #800080;">name<span style="color: #000000;">)
app.secret_key=<span style="color: #800000;">'<span style="color: #800000;">afjlsjfowflajflkajfkjfkaljf<span style="color: #800000;">'<span style="color: #000000;">
user_list = [<span style="color: #800000;">'<span style="color: #800000;">jim<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">max<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">py<span style="color: #800000;">'<span style="color: #000000;">]
imagepath = os.path.join(os.getcwd(),<span style="color: #800000;">"<span style="color: #800000;">static/images<span style="color: #800000;">"<span style="color: #000000;">)
@app.route(<span style="color: #800000;">'<span style="color: #800000;">/<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">def<span style="color: #000000;"> index():
username = request.cookies.get(<span style="color: #800000;">'<span style="color: #800000;">username<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">if <span style="color: #0000ff;">not<span style="color: #000000;"> username:
username = u<span style="color: #800000;">'<span style="color: #800000;">请先登录<span style="color: #800000;">'<span style="color: #000000;">
islogin = session.get(<span style="color: #800000;">'<span style="color: #800000;">islogin<span style="color: #800000;">'<span style="color: #000000;">)
nav_list = [u<span style="color: #800000;">'<span style="color: #800000;">首页<span style="color: #800000;">',u<span style="color: #800000;">'<span style="color: #800000;">经济<span style="color: #800000;">',u<span style="color: #800000;">'<span style="color: #800000;">文化<span style="color: #800000;">',u<span style="color: #800000;">'<span style="color: #800000;">科技<span style="color: #800000;">',u<span style="color: #800000;">'<span style="color: #800000;">娱乐<span style="color: #800000;">'<span style="color: #000000;">]
blog = {<span style="color: #800000;">'<span style="color: #800000;">title<span style="color: #800000;">':<span style="color: #800000;">'<span style="color: #800000;">welcome to my blog<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">content<span style="color: #800000;">':<span style="color: #800000;">'<span style="color: #800000;">hello,welcome to my blog.<span style="color: #800000;">'<span style="color: #000000;">}
blogtag = {<span style="color: #800000;">'<span style="color: #800000;">javascript<span style="color: #800000;">':10,<span style="color: #800000;">"<span style="color: #800000;">python<span style="color: #800000;">":20,<span style="color: #800000;">"<span style="color: #800000;">shell<span style="color: #800000;">":5<span style="color: #000000;">}
img = url_for(<span style="color: #800000;">'<span style="color: #800000;">static<span style="color: #800000;">',filename=<span style="color: #800000;">"<span style="color: #800000;">images/cat.jpg<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">return render_template(<span style="color: #800000;">'<span style="color: #800000;">index.html<span style="color: #800000;">',nav_list=nav_list,username=username,blog = blog,blogtag = blogtag,img=img,islogin=<span style="color: #000000;">islogin)
@app.route(<span style="color: #800000;">'<span style="color: #800000;">/reg<span style="color: #800000;">',methods=[<span style="color: #800000;">'<span style="color: #800000;">GET<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">POST<span style="color: #800000;">'<span style="color: #000000;">])
<span style="color: #0000ff;">def<span style="color: #000000;"> regist():
<span style="color: #0000ff;">if request.method == <span style="color: #800000;">'<span style="color: #800000;">POST<span style="color: #800000;">'<span style="color: #000000;">:
username = request.form[<span style="color: #800000;">'<span style="color: #800000;">username<span style="color: #800000;">'<span style="color: #000000;">]
conn = MySQLdb.connect(user=<span style="color: #800000;">'<span style="color: #800000;">root<span style="color: #800000;">',passwd=<span style="color: #800000;">'<span style="color: #800000;">admin<span style="color: #800000;">',host=<span style="color: #800000;">'<span style="color: #800000;">127.0.0.1<span style="color: #800000;">'<span style="color: #000000;">)
conn.select_db(<span style="color: #800000;">'<span style="color: #800000;">blog<span style="color: #800000;">'<span style="color: #000000;">)
curr =<span style="color: #000000;"> conn.cursor()
sql = <span style="color: #800000;">'<span style="color: #800000;">insert into user
(id
,username
) values (%d,"%s")<span style="color: #800000;">' % (1<span style="color: #000000;">,username)
curr.execute(sql)
conn.commit()
curr.close()
conn.close()
<span style="color: #0000ff;">return <span style="color: #800000;">"<span style="color: #800000;">user %s regist ok!<span style="color: #800000;">" % request.form[<span style="color: #800000;">'<span style="color: #800000;">username<span style="color: #800000;">'<span style="color: #000000;">]
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #008000;">#<span style="color: #008000;">request.args['username']
<span style="color: #0000ff;">return render_template(<span style="color: #800000;">'<span style="color: #800000;">regist.html<span style="color: #800000;">'<span style="color: #000000;">)
@app.route(<span style="color: #800000;">'<span style="color: #800000;">/upload<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">POST<span style="color: #800000;">'<span style="color: #000000;">])
<span style="color: #0000ff;">def<span style="color: #000000;"> upload():
<span style="color: #0000ff;">if request.method == <span style="color: #800000;">'<span style="color: #800000;">POST<span style="color: #800000;">'<span style="color: #000000;">:
username = request.form[<span style="color: #800000;">'<span style="color: #800000;">username<span style="color: #800000;">'<span style="color: #000000;">]
file = request.files[<span style="color: #800000;">'<span style="color: #800000;">img<span style="color: #800000;">'<span style="color: #000000;">]
filename =<span style="color: #000000;"> file.filename
file.save(os.path.join(imagepath,filename))
<span style="color: #0000ff;">return <span style="color: #800000;">"<span style="color: #800000;">
<span style="color: #800000;">" %<span style="color: #000000;"> filename
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #0000ff;">return render_template(<span style="color: #800000;">'<span style="color: #800000;">upload.html<span style="color: #800000;">'<span style="color: #000000;">)
@app.route(<span style="color: #800000;">'<span style="color: #800000;">/login/<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">POST<span style="color: #800000;">'<span style="color: #000000;">])
<span style="color: #0000ff;">def<span style="color: #000000;"> login():
<span style="color: #0000ff;">if request.method == <span style="color: #800000;">'<span style="color: #800000;">POST<span style="color: #800000;">'<span style="color: #000000;">:
username = request.form.get(<span style="color: #800000;">'<span style="color: #800000;">username<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">if username <span style="color: #0000ff;">in<span style="color: #000000;"> user_list:
response = make_response(redirect(<span style="color: #800000;">'<span style="color: #800000;">/<span style="color: #800000;">'<span style="color: #000000;">))
response.set_cookie(<span style="color: #800000;">'<span style="color: #800000;">username<span style="color: #800000;">',value=username,max_age=300<span style="color: #000000;">)
session[<span style="color: #800000;">'<span style="color: #800000;">islogin<span style="color: #800000;">'] = <span style="color: #800000;">'<span style="color: #800000;">1<span style="color: #800000;">'
<span style="color: #0000ff;">return<span style="color: #000000;"> response
<span style="color: #0000ff;">else<span style="color: #000000;">:
session[<span style="color: #800000;">'<span style="color: #800000;">islogin<span style="color: #800000;">'] = <span style="color: #800000;">'<span style="color: #800000;">0<span style="color: #800000;">'
<span style="color: #0000ff;">return redirect(<span style="color: #800000;">'<span style="color: #800000;">/login/<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #0000ff;">return render_template(<span style="color: #800000;">'<span style="color: #800000;">login.html<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">if <span style="color: #800080;">name == <span style="color: #800000;">'<span style="color: #800000;">main<span style="color: #800000;">'<span style="color: #000000;">:
app.run(debug=True,host=<span style="color: #800000;">'<span style="color: #800000;">0.0.0.0<span style="color: #800000;">',port=5000)
blog.py主要是展示了Flask中常见功能用法:路由,数据库操作,cookie,session,redirect,表单,文件上传,调试,Web服务器的IP和端口,静态文件读取等。