用户登陆系统,采用Flask,SQLite,hmac编写

# -*- 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()

转载于:https://www.cnblogs.com/LewisAAA/p/9255982.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值