注册-登录-数据都存在数据库里面-注册的时候,密码存的是加密之后的密码-登录成功之后打印当前的日期

# 3、 注册
# 登录
# 数据都存在数据库里面
# idusernamepasswd
# 注册的时候,密码存的是加密之后的密码
# usernamepwdcpwd, 都是必填的
# 用户不能重复
# 登录
# 账号
# 密码
# 登录成功之后打印当前的日期

import hashlib,pymysql,datetime
def my_db(sql):
import pymysql
coon = pymysql.connect(
host='118.xx.xx.xx', user='xxx', passwd='123456',
port=3306, db='xxx', charset='utf8')
cur = coon.cursor() #建立游标
cur.execute(sql)#执行sql
if sql.strip()[:6].upper()=='SELECT':
res = cur.fetchall()
else:
coon.commit()
res = 'ok'
cur.close()
coon.close()
return res

def my_md5(str):
import hashlib
new_str = str.encode() #把字符串转成bytes类型
# new_str = b'%s'%str #把字符串转成bytes类型
m = hashlib.md5() #实例化md5对象
m.update(new_str) #加密
return m.hexdigest() #获取结果返回

def reg():
username = input('username:').strip()
pwd = input('pwd:').strip()
cpwd = input('cpwd:').strip()
if username and pwd and cpwd:
sql = 'select * from nhy where name="%s";'%username
# select * from nhy where name='nhy';
res = my_db(sql)
if res:
print('该用户已经存在!')
else:
if pwd == cpwd:
md5_pwd = my_md5(pwd)
insert_sql = 'insert into nhy (name,pwd) value ("%s","%s");'%(username,md5_pwd)
my_db(insert_sql)
print('注册成功!')
else:
print('两次输入的密码不一致')
else:
print('必填项不能为空!')

def login():
username = input('username:').strip()
pwd = input('pwd:').strip()
if username and pwd:
md5_pwd = my_md5(pwd)
sql = 'select * from nhy where name="%s" and pwd="%s";'%(username,md5_pwd)
# select * from nhy where name='nhy';
res = my_db(sql)
if res:
print('欢迎,登录成功!今天是%s'%datetime.date.today())
else:
print('账号/密码错误!')
else:
print('必填项不能为空!')
login()

原文地址:https://www.cnblogs.com/jiadan/p/9033457.html