zoukankan      html  css  js  c++  java
  • 注册-登录-数据都存在数据库里面-注册的时候,密码存的是加密之后的密码-登录成功之后打印当前的日期

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

  • 相关阅读:
    hdu 4002 Find the maximum
    hdu 2837 坑题。
    hdu 3123
    zoj Treasure Hunt IV
    hdu 2053 Switch Game 水题一枚,鉴定完毕
    poj 1430 Binary Stirling Numbers
    hdu 3037 Saving Beans
    hdu 3944 dp?
    南阳oj 求N!的二进制表示最低位的1的位置(从右向左数)。
    fzu 2171 防守阵地 II
  • 原文地址:https://www.cnblogs.com/jiadan/p/9033457.html
Copyright © 2011-2022 走看看