Python operation database and excel exercise 2_registration and login

need:

1. Registration:

The data is stored in the database (reading the database)
when registering, the password is the encrypted password (md5 encryption)
username, pwd, cpwd are required and the
user cannot be repeated

2. The login
account and password The login
input is in plain text, and the database is in cipher text.
After the login is successful, the current date is printed.

accomplish:

import pymysql,hashlib,datetime

def execute_sql(sql): #Connect database 
    conn = pymysql.connect(host= ' xxx.xx.x.xx ' ,user= ' xxx ' ,passwd= ' 123456 ' ,db= ' xx ' ,port=3306,charset = ' utf8 ' )
    cur = conn.cursor()
    cur.execute(sql)
    conn.commit()
    res = cur.fetchall()
    cur.close()
    conn.close()
    return res

def MD5(passwd): # MD5 encryption 
    m = hashlib.md5()
    m.update(passwd.encode())
    return m.hexdigest()

def user_exist(username): #Determine whether the user exists 
    info = execute_sql( ' select * from dongrui where username = "%s" ' % username)
     return info

def register(): #Register 
    username = input( ' Enter username ' ) .strip ()
    passwd = input( ' input password ' ).strip()
    cpasswd = input( ' Confirm password ' ).strip()
     if username == ''  or passwd == ''  or cpasswd == '' :
         print ( ' Username and password cannot be empty ' )
     elif passwd != cpasswd:
         print ( ' The passwords entered twice are inconsistent ' )
     elif user_exist(username):
         print ( ' The username already exists ' )
     else :
        passwd_m = MD5(passwd)
        ID = excute_sql('select count(*) from dongrui')[0][0] + 1
        excute_sql('insert into dongrui VALUES("%s","%s","%s")' % (ID, username, passwd_m))
        print('注册成功')

def login(): #Login username 
    = input( ' Enter username ' ).strip()
    passwd = input( ' Enter password ' ).strip()
     if username == ''  or passwd == '' :
         print ( ' Username and password cannot be empty ' )
     elif user_exist(username) and user_exist(username)[0] [2] == MD5(passwd):
         print ( ' Login successful, welcome %s, today is %s ' % (username, datetime.date.today()))
     else :
         print ( ' Username or password is incorrect ' )

choice = input( ' Enter your choice: 1, register, 2, log in ' )
 if choice == ' 1 ' :
    register()
elif choice == '2':
    login()
else :
     print ( ' input error ' )

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325595884&siteId=291194637