zoukankan      html  css  js  c++  java
  • 用户名和密码注册、登陆系统,SQLite数据库版本

    import hashlib
    import pickle, os
    import hmac, random
    import sqlite3


    def hmac_sha1(key, s):
    return hmac.new(key.encode('utf-8'), s.encode('utf-8'), 'MD5').hexdigest()


    conn = sqlite3.connect('user.db')
    cursor = conn.cursor()
    try:
    cursor.execute('create table userpass (name varchar(20) primary key, password varchar(20))')
    except Exception as e:
    pass

    # 创建或者连接数据库
    # conn = sqlite3.connect('user.db')
    # cursor = conn.cursor()
    # try:
    # cursor.execute('create table userpass (name varchar(20), password varchar(20))')
    # except Exception as e:
    # print('Error: ', e)

    # 获取用户名和密码信息
    # if os.path.exists('/Users/lewisliu/user_information.txt'):
    # with open('/Users/lewisliu/user_information.txt', 'rb') as f:
    # user_information = pickle.load(f)
    # else:
    # user_information = dict()

    # 注册或者登陆
    while True:
    # conn = sqlite3.connect('user.db')
    # cursor = conn.cursor()
    # try:
    # cursor.execute('create table userpass (name varchar(20) primary key, password varchar(20))')
    # except Exception as e:
    # pass
    # print('Error: ', e)

    # 选择模式,注册、登陆、退出
    model = input("pleas input enroll/login/exit:")
    if model == 'enroll':
    name = input("please input your name:")
    # 用户名重复识别
    flag = cursor.execute("select name from userpass where name='%s'" % name).fetchone()
    if flag:
    print('name is exist!')
    else:
    password = input("please input your password:")
    key = 'liu' # ''.join([chr(random.randint(48, 122)) for i in range(20)])
    password = hmac_sha1(key, password)
    cursor.execute("insert into userpass (name, password) values ('%s','%s')" % (name, password))
    conn.commit()
    # conn.close()
    # if name in user_information.keys():
    # print("name is occupied!")
    # continue
    # password = input("please input your password:")
    # # 数据库添加用户名和密码
    # key = 'liu' # ''.join([chr(random.randint(48, 122)) for i in range(20)])
    # user_information[name] = hmac_sha1(key, password)
    # sha1 = hashlib.sha1()
    # sha1.update(password.encode('utf-8'))
    # user_information[name] = sha1.hexdigest()
    elif model == 'login':
    name = input("please input your name:")
    flag = cursor.execute("select name from userpass where name='%s'" % name).fetchone()
    if flag:
    password = input("please input your password:")
    key = 'liu'
    password = hmac_sha1(key, password)
    flag = cursor.execute("select name and password from userpass where name = '%s' and password='%s'" % (name, password)).fetchone()
    if flag:
    print("Wellcome!")
    else:
    print("wrong password! Exit!")
    conn.close()
    exit()
    else:
    print("name is not exist!")
    # if name not in user_information.keys():
    # # print("name is not exist!")
    # # continue
    # # password = input("please input your password:")
    # # key = 'liu' # ''.join([chr(random.randint(48, 122)) for i in range(20)])
    # # password = hmac_sha1(key, password)
    # # # sha1 = hashlib.sha1()
    # # # sha1.update(password.encode('utf-8'))
    # # # password = sha1.hexdigest()
    # # if user_information[name] == password:
    # # print("Wellcome!")
    # # else:
    # # print("wrong name or password! Exit!")
    # # with open('/Users/lewisliu/user_information.txt', 'wb') as f:
    # # pickle.dump(user_information, f)
    # # exit()
    elif model == 'exit':
    conn.close()
    exit()
    # with open('/Users/lewisliu/user_information.txt', 'wb') as f:
    # pickle.dump(user_information, f)
    # exit()
    else:
    print("model wrong!")
  • 相关阅读:
    大量 TIME_WAIT 状态 TCP 连接,对业务有什么影响?怎么处理
    点击按钮保存当前页面为图片html2canvas
    浅谈webpack优化
    nginx配置
    table表格边框线问题
    git push到指定仓库
    No bean named 'xxx' is defined错误,原因及解决方案
    关于The requested list key 'map' could not be resolved as a collection/array/map/enumera...
    Tomcat网页加载过慢的排查调整time_wait连接过多
    MySQL information_schema 系统库
  • 原文地址:https://www.cnblogs.com/LewisAAA/p/9250875.html
Copyright © 2011-2022 走看看