Django Pylons Tornado web.py
Flask Bottle 这些框架我们不作详细介绍,感兴趣的同学去网站看下文档。
前后端交互的案例当中,最常见的就是表单的提交,我们以登陆验证为例,简单的介绍Bottle的使用,废话不多说,直接上代码。 #!python
#coding:utf-8
#author:kim
#copyrights 2017 www.lowpitch.cn all rights reserved.
from bottle import get, post, run, request, route
import MySQLdb
import hashlib
@route('/')
@get('/login') # or @route('/login')
def login():
return '''
Username:
Password:
'''
@post('/login') # or @route('/login', method='POST')
def do_login():
username = request.forms.get('username')
password = request.forms.get('password')
if check_login(username, password):
return '
Your login information was correct.
' else:
return '
Login failed.
' def check_login(username,password):
db = MySQLdb.connect('localhost','root')
cursor =db.cursor()
cursor.execute('use test;')
cursor.execute('select count(*) from usr where usr='%s' and psw='%s' ' \
%(username,hashlib.md5(password.encode('utf-8')).hexdigest()))
status= cursor.fetchone()
db.close()
return status
run(host='localhost', port=8080)
|