1. 程式人生 > >Python簡單的登錄註冊代碼

Python簡單的登錄註冊代碼

def 定義數據 gis 登錄註冊 hash %u 簡單的 用戶名 python

#-*- coding: utf-8 -*-
import hashlib
# 定義數據庫(聲明字典)
#註冊登錄的簡單hash處理
db={}
def get_md5(password):
md5=hashlib.md5()
#此處密碼hash加密處理
md5.update(password.encode(‘utf-8‘))
return md5.hexdigest()

def register(username,password):
pwd=db.get(username,-1)
if pwd==-1:
db[username]=get_md5(username+password+‘123456‘)
print(‘註冊成功!‘)
print(‘user:%s‘%username)
print(‘md5:%s‘%db[username])
else:
print(‘用戶已經存在!‘)
def login(username,password):
pwd=db.get(username,-1)#db.get()方法是獲取value,若不存在返回-1
if pwd==-1:
print(‘用戶不存在!‘)
elif get_md5(username+password+‘123456‘)!=pwd:
print(‘用戶名或密碼不正確!‘)
else:
print(‘歡迎您,%s‘%username)
print(‘開始註冊!‘)
user=input(‘user:‘)
password=input(‘password:‘)
register(user,password)
print(‘開始登陸!‘)
user=input(‘user:‘)
password=input(‘password:‘)
login(user,password)

Python簡單的登錄註冊代碼