zoukankan      html  css  js  c++  java
  • 用户登陆系统,采用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()
  • 相关阅读:
    kmeans 初步学习小结
    CAVASS使用经验
    分类之数据集导入matlab方法
    彩色图转化成灰度图
    阈值分割之迭代选择阈值法
    初步学习之FCM
    特征提取学习之HOG原理讲解
    特征提取初步学习之LBP算法
    CodePen.io网站前端设计开发平台
    阿里负责人揭秘面试潜规则
  • 原文地址:https://www.cnblogs.com/LewisAAA/p/9255982.html
Copyright © 2011-2022 走看看