# -*- coding: utf-8 -*-
"""
Created on Sat Jun 30 10:56:19 2018
@author: lewisliu
"""
from flask import Flask
from flask import request
import sqlite3
import hmac
#def hmac_sha1(key, s):
# return hmac.new(key.encode('utf-8'), s.encode('utf-8'), 'MD5').hexdigest()
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def home():
return '<h1>Home</h1>'
@app.route('/signin', methods=['GET'])
def signin_form():
return'''<form action="/signin" method="post">
<p>model<input name="model">(enroll/login/exit)</p>
<p>username<input name="username"></p>
<p>password<input name="password" type="password"></p>
<p><button type="submit">Submit</button></p>
</form>'''
@app.route('/signin', methods=['POST'])
def signin():
# 需要从request对象读取表单内容:
conn = sqlite3.connect('user_flask.db')
cursor = conn.cursor()
try:
cursor.execute('create table userpass (name varchar(20) primary key, password varchar(20))')
except Exception as e:
pass
if request.form['model']=='exit':
conn.close()
return'<h3>Good bay!</h3>'
elif request.form['model']=='enroll':
flag = cursor.execute("select name from userpass where name='%s'" % request.form['username']).fetchone()
if flag:
return'''<form action="/signin" method="post">
<p>name exit!</p>
<p>model<input name="model">(enroll/login/exit)</p>
<p>username<input name="username"></p>
<p>password<input name="password" type="password"></p>
<p><button type="submit">Submit</button></p>
</form>'''
else:
key = 'liu' # ''.join([chr(random.randint(48, 122)) for i in range(20)])
password = hmac.new(key.encode('utf-8'), request.form['password'].encode('utf-8'), 'MD5').hexdigest()
cursor.execute("insert into userpass (name, password) values ('%s','%s')" % (request.form['username'], password))
conn.commit()
return'''<form action="/signin" method="post">
<p>you enroll successful!</p>
<p>model<input name="model">(enroll/login/exit)</p>
<p>username<input name="username"></p>
<p>password<input name="password" type="password"></p>
<p><button type="submit">Submit</button></p>
</form>'''
elif request.form['model']=='login':
key = 'liu'
password = hmac.new(key.encode('utf-8'), request.form['password'].encode('utf-8'), 'MD5').hexdigest()
flag = cursor.execute("select name and password from userpass where name = '%s' and password='%s'" % (request.form['username'], password)).fetchone()
if flag:
return'''<form action="/signin" method="post">
<p>wellcome!</p>
<p>model<input name="model">(enroll/login/exit)</p>
<p>username<input name="username"></p>
<p>password<input name="password" type="password"></p>
<p><button type="submit">Submit</button></p>
</form>'''
else:
return'''<form action="/signin" method="post">
<p>Bad username or password!</p>
<p>model<input name="model">(enroll/login/exit)</p>
<p>username<input name="username"></p>
<p>password<input name="password" type="password"></p>
<p><button type="submit">Submit</button></p>
</form>'''
else:
return'''<form action="/signin" method="post">
<p>model wrong!</p>
<p>model<input name="model">(enroll/login/exit)</p>
<p>username<input name="username"></p>
<p>password<input name="password" type="password"></p>
<p><button type="submit">Submit</button></p>
</form>'''
if __name__ == '__main__':
app.run()