您的位置:首页 > 运维架构 > Nginx

ubuntu下使用nginx、mysql、python搭建一个简单的webserver

2016-11-21 08:46 726 查看
所有源码均在百度云盘的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

<body>
<!--正文-->
<div id="logo">
</div>
<div id="wrap">
<h4>用户登录</h4>
<div id="reg_box">
<form id="loginform" name="login" method="post" action="">
{{loginform.hidden_tag()}}
<ul class="input_ul clearfix">
<li>
<div class="fl">用户名:</div><input class="form-control" datatype="*" name="name" type="text">
<span class="Validform_checktip"></span></li>

<li>
<div class="fl">密码:</div><input class="form-control" datatype="*" name="password" type="password">
<span class="Validform_checktip"></span></li>
<li class="mb0">
<div class="fl lin">  </div>
<a href="/register" class="btn">新用户注册</a>
</li>
</ul>
<input class="reg_able" value="登录" type="submit">
</form>
<br>
<br>
<font color="red">
<h3>{{get_flashed_messages()[0]}}</h3>
</font>
</div>
</div>
<div class="clearfix"></div>

<script type="text/javascript">
$(function() {
$("#loginform").Validform({
tiptype: 3,
showAllError: true,
});
})

</script>
</body>


需要使用的js代码放置在/static/目录下

<script src="/static/jquery.js"></script>
<script src="/statics/bootstrap.js"></script>
<script src="/static/Validform_v5.js"></script>


服务器端使用python开发,代码如下

#!/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")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息