码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2018-07-02 23:03:31      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:sel   exce   login   app   sha1   commit   rom   cursor   BMI   

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

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

标签:sel   exce   login   app   sha1   commit   rom   cursor   BMI   

原文地址:https://www.cnblogs.com/LewisAAA/p/9255982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!