登录和注册,要自己建user表,注册进来的密码要加密加盐1

#select * from user where username="%s";' % username

#登陆

import hashlib
import string, datetime

import pymysql


# 检查用户是否在数据库中
def check_user_exist(username):
sql = "select pw_md5 from %s where uname = '%s'" % (table_name, username)
cur.execute(sql)
db = cur.fetchall()
if db:
return db[0].get('pw_md5') # 如果用户存在,返回该用户的加密密码
else:
return None


# 给密码加密加盐
def my_md5(s, salt=""):
new_s = str(s) + salt
m = hashlib.md5(new_s.encode())
return m.hexdigest()


def check_password(password):
return set(password) & set(string.ascii_uppercase) and set(password) & \
set(string.ascii_lowercase) and set(password) & set(string.digits) \
and len(password) >= 8 and len(password) <= 12


def check_username(username):
return len(username) >= 6 and len(username) <= 12


mysql_info = {
"host": "118.24.3.140",
"user": "jxz",
"password": "123456",
"port": 3306,
"charset": "utf8",
"autocommit": True,
'db': 'jxz'
}

conn = pymysql.connect(**mysql_info) # 连接数据库,必须加**,因为是多个参数
cur = conn.cursor(pymysql.cursors.DictCursor)
table_name = 'users'

for i in range(3):
username = input("username:").strip()
password = input("password:").strip()
if not check_username(username):
print("用户名必须长度必须6-12")
continue
if not check_password(password):
print("密码必须长度必须8-12,必须包含大小写字母、数字")
continue
if check_user_exist(username):
if my_md5(password) == check_user_exist(username):
print("登录成功!今天的日期是 %s" % datetime.datetime.today())
break
else:
print("密码错误!")
else:
print("用户不存在")

else:
print("错误次数过多,最多可以输入3次")
cur.close()
conn.close()
posted @ 2021-01-29 12:02  术成  阅读(118)  评论(0)    收藏  举报