所有源码均在百度云盘的webserver压缩包。
1.nginx的环境配置可以自行百度,教程很多。
2.python使用mysql的方法可以参考下面的博客
http://www.cnblogs.com/fnng/p/3565912.html
安装MySQL-python驱动:sudo apt-get install python-mysqldb
3.基于flask的web开发,可以参考下面的博客
http://blog.csdn.net/agmcs/article/details/45308431
定义一个表单模板
/templas/login.html
用户登录
{{get_flashed_messages()[0]}}
#!/usr/bin/env python
#--* coding:UTF-8 --*
import flask
from flask import Flask,flash,render_template,request
app = Flask(__name__)
app.secret_key = '123'
from loginform import LoginForm
from registerform import RegisterForm
import os
import re
import MySQLdb
@app.route('/login', methods=('GET', 'POST')) #, methods=('GET', 'POST')
def login():
form = LoginForm()
#验证表单输入内容是否符合要求
if form.validate_on_submit():
#获取表单提交的内容
#return "name:%r,password:%r"%(form.name.data,form.password.data)
name = form.name.data
password = form.password.data
name_flag = check_name(name)
if name_flag:
pwd_flag = check_pwd(name,password)
if pwd_flag:
return flask.render_template('homepage.html')
return flask.render_template('login.html', loginform=form)
def check_name(username):
zhPattern = re.compile(u'[\u4e00-\u9fa5]+')
match = zhPattern.search(username)
if match:
flash(u"错误:用户名不支持中文")
return False
else:
if len(username)==0:
flash(u"错误:请输入用户名")
return False
else:
if len(username) > 10:
flash(u"错误:用户名最大长度为10个字符,已超过")
return False
else:
#flash(username)
return True
def check_pwd(username,password):
conn = MySQLdb.connect(host='localhost',port=3306,user='root',passwd='root',db='cndroid_schema')
cur = conn.cursor()
count = cur.execute("select * from user_identity where name='"+username+"'")
if count == 0:
flash(u"错误:用户名不存在")
return_res = False
else:
result = cur.fetchone()
if result[1] == password:
return_res = True
else:
flash(u"错误:密码错误")
return_res = False
cur.close()
conn.commit()
conn.close()
return return_res
@app.route('/register', methods=('GET', 'POST'))
def register():
form = RegisterForm()
if form.validate_on_submit():
name = form.name.data
password = form.password.data
con_password = form.password1.data
name_flag = check_name(name)
if name_flag:
pwd_flag = confirm_pwd(password,con_password)
if pwd_flag:
write_flag = write_mysql(name,password)
if write_flag:
return flask.render_template('homepage.html')
return flask.render_template('register.html', registerform=form)
def confirm_pwd(password,password1):
zhPattern = re.compile(u'[\u4e00-\u9fa5]+')
match = zhPattern.search(password)
if match:
flash(u"错误:密码不支持中文")
return False
else:
if len(password)<6 or len(password)>20:
flash(u"错误:密码必须为6-20位字符和数字组合")
return False
else:
if password1 != password:
flash(u"错误:两次输入的密码不一致")
return False
else:
return True
def write_mysql(username,password):
conn = MySQLdb.connect(host='localhost',port=3306,user='root',passwd='root',db='cndroid_schema')
cur = conn.cursor()
count = cur.execute("select * from user_identity where name='"+username+"'")
if count != 0:
flash(u"错误:用户名已经存在")
return_res = False
else:
count1 = cur.execute("insert into user_identity values('"+username+"','"+password+"',-1)")
if count1 == 0:
flash(u"错误:数据库访问失败")
return_res = False
else:
return_res = True
cur.close()
conn.commit()
conn.close()
return return_res
if __name__ == '__main__':
app.debug = True
app.config['SECRET_KEY'] = os.urandom(12).encode('hex')
app.run(host='0.0.0.0',port=5000)
@app.route('/login', methods=('GET', 'POST'))
定义多个路由,注意表单中的名字和模板中的要一致,表单的定义如下
/loginform.py
#!/usr/bin/env python
#from __future__ import absolute_import
#import wtf_form
from flask.ext.wtf import Form
from wtforms import StringField, PasswordField, SubmitField,ValidationError
from wtforms.validators import Required, EqualTo
class LoginForm(Form):
name = StringField('name',validators=[Required()])
password = PasswordField('password', validators=[Required()])
submit = SubmitField("submit")