微型用户信息管理系统MUIMS(Micro User Information Management System)V1.0

Posted wy_0928

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微型用户信息管理系统MUIMS(Micro User Information Management System)V1.0相关的知识,希望对你有一定的参考价值。

【说明】自学Python,初学者,图形化界面暂时没学习到,这部分没做,以下是简单的用户信息管理系统含有的功能:

(1)新用户注册(r):会判断用户名是否合法,这里只考虑了用户名只能包含字母和下划线且长度小于等于9,其他的暂时没考虑;

(2)老用户登陆(l):如果距离上一次登陆间隔小于4小时,会给出提示,如果输入一个不存在的用户名,会询问是否新用户,选y则进入注册账号,选n则继续登陆已有账号;

(3)查看用户名和密码(管理员专属)(v):选y查看所有用户名和密码,选n查看指定用户名和密码,选q退出当前选项;

(4)删除老用户(管理员专属)(d):选y确定删除,选n不删除;

(5)老用户修改密码(c)

(6)退出本程序(q)

(7)其他:密码双重加密,破解可能性几乎为零,用户名忽略大小写……

代码如下,写的比较粗糙,而且没优化,打算后期有空再来修改代码。

#-*-coding:utf-8-*-
#!/usr/bin/env python

import datetime
import string
import base64
import hashlib

db_namepwd = 
db_logtime = 

#判断新用户注册的用户名是否合法:(1)只能由字母和数字组成,字母忽略大小写;(2)长度小于等于9
def checkUserName(usr_name):
    check_str = string.letters[:26] + string.digits
    name_len = len(usr_name)
    if name_len > 9:
        check_re = False
    else:
        for i in range(0,name_len):
            if usr_name[i] in check_str:
                check_re = True
            else:
                check_re = False
                break
    return check_re

#密码双重加密,几乎无法破解
def encodePsw(psw):
    m = hashlib.md5()
    psw_encode = base64.b64encode(psw)
    m.update(psw_encode)
    return m.hexdigest()

#薪用户注册
def myRegister():
    while True:
        name = raw_input('Please enter a username: ').strip().lower()
        if checkUserName(name) == False:
            print 'Invalid username or username is too long! Please try again!'
            continue
        if db_namepwd.has_key(name):
            print 'This name is already used! Please try another: '
            continue
        else:
            break
    pwd = raw_input('Pleas enter your passwd: ')
    print 'Register finished!'
    db_namepwd[name] = encodePsw(pwd)

#老用户登陆
def myLogin():
    log_choice = ''
    while True:
        log_name = raw_input('username: ').lower()
        if log_name not in db_namepwd.keys():
            while True:
                print 'This username doesen\\'t exists! Are you a new user? Please choose "y" or "n"!'
                try:
                    log_choice = raw_input('Are you a new user? ').strip().lower()
                except (IOError, KeyboardInterrupt):
                    continue
                if log_choice not in 'yn':
                    print 'Error choose! Are you a new user?'
                else:
                    break
            if log_choice == 'y':
                print 'Please register a new account!'
                myRegister()
                print 'Please login by your new account!'
                continue
            else:
                print 'Please login your account!'
                continue
        else:
            log_pwd = encodePsw(raw_input('passwd: '))
            passwd = db_namepwd.get(log_name)
        if passwd == log_pwd:
            print 'welcome back',log_name
            break
        else:
            print 'login incorrect! Please try again!'
    return log_name

#新增:判断用户上次登陆和本次登陆时间差是否在4h之内
def judge4hTime(usr_name):
    if db_logtime.has_key(usr_name):
        last_logtime = db_logtime[usr_name]
        this_logtime = datetime.datetime.now()
        temp_time = last_logtime + datetime.timedelta(hours=4)
        if this_logtime <= temp_time:
            print 'In the past 4 hours, you\\'ve already login!'
            print 'Your last logtime is: ', last_logtime
        db_logtime[usr_name] = this_logtime
    else:
        db_logtime[usr_name] = datetime.datetime.now()

#老用户修改密码功能
def changePsw():
    origin_name = raw_input('Please enter your username:').strip().lower()
    if origin_name not in db_namepwd.keys():
        print 'This username does\\'t exists!'
    else:
        origin_psw = encodePsw(raw_input('Please enter your original password:'))
        if origin_psw == db_namepwd[origin_name]:
            new_psw1 = encodePsw(raw_input('Please enter your new password:'))
            if new_psw1 == origin_psw:
                print '[Error!] New password is the same as old password!'
            else:
                new_psw2 = encodePsw(raw_input('Please enter your new password again:'))
                if new_psw1 == new_psw2:
                    print 'Change password finished!'
                    db_namepwd[origin_name] = new_psw1
                else:
                    print 'Change password failed! Two new passwords are different!'
        else:
            print 'Change password failed! You may forget your password! Please try again!'

#删除用户
def delUser():
    print 'Are you sure to delete an user? Please choose "y" or "n"'
    while True:
        try:
            del_choice = raw_input('Please enter your decision! ').strip().lower()
        except (IOError, KeyboardInterrupt):
            del_choice = 'n'
        if del_choice not in 'yn':
            print 'Error choose! You must choose one from "y" and "n"! Please Try again!'
        else:
            break
    if del_choice == 'y':
        del_name = raw_input('Which user do you want to delete?').strip().lower()
        if del_name not in db_namepwd.keys():
            print 'This user does not exists!'
        else:
            try:
                db_namepwd.pop(del_name)
                db_logtime.pop(del_name)
                print 'Delete user', del_name, 'finished'
            except (KeyError):
                print 'Delete user', del_name, 'finished'

#查看用户名和密码
def viewUser():
    print 'Do you want to view all users? Please choose one from "y", "n", "q"!'
    view_choice = ''
    while True:
        while True:
            try:
                view_choice = raw_input("Please choose: ").strip().lower()
            except (IOError, KeyboardInterrupt):
                view_choice = 'q'
            if view_choice not in 'ynq':
                print 'Error choose! You must choose one from "y" and "n"! Please try again!'
            else:
                break
        if view_choice == 'q':
            break
        if view_choice == 'y':
            user_num = len(db_namepwd)
            print 'This system has',user_num,'users! Every user\\'s name and password is below: '
            for i in range(0,user_num):
                name = db_namepwd.keys()[i]
                psw = db_namepwd[name]
                print 'Username:',name,', Password:',psw
        if view_choice == 'n':
            view_name = raw_input('Which user do you want to view?').strip().lower()
            if view_name not in db_namepwd.keys():
                print 'This user does not exists!'
            else:
                print 'Username:',view_name,', Password:',db_namepwd[view_name]

#"主"函数
def myShow():
    #系统一启动,自动生成管理员账号密码
    super_name = 'admin'
    super_psw = raw_input('************Please enter the password for administrator!************\\n')
    db_namepwd[super_name] = encodePsw(super_psw)
    choice = 'q'
    while True:
        while True:
            try:
                choice = raw_input('Please choose:').strip().lower()
            except (IOError, KeyboardInterrupt):
                choice = 'q'
            if choice not in 'rldvcq':
                print 'Error choose! You must choose one from "r","l","d","v","c","q"! Try again!'
            else:
                break
        if choice == 'q':
            print 'We always waiting for you!'
            break
        if choice == 'r':
            print 'Please register!'
            myRegister()
        if choice == 'l':
            print 'Please login!'
            name = myLogin()
            judge4hTime(name)
        if choice == 'v':
            #只有管理员才能查看系统中所有用户
            print '[Notice!]Only administrator can use this function!'
            try_name = raw_input('Administrator username: ').strip().lower()
            try_psw = encodePsw(raw_input('Administrator password: '))
            if try_name == super_name and try_psw == db_namepwd[try_name]:
                viewUser()
            else:
                print '[Warning!]You have no permission!'
        if choice == 'd':
            #只有管理员才能删除用户
            print '[Notice!]Only administrator can use this function!'
            try_name = raw_input('Administrator username: ').strip().lower()
            try_psw = encodePsw(raw_input('Administrator password: '))
            if try_name == super_name and try_psw == db_namepwd[try_name]:
                delUser()
            else:
                print '[Warning!]You have no permission!'
        if choice == 'c':
            print 'Please change your password!'
            changePsw()
        print '==========================Next Choose=========================='

if __name__ == '__main__':
    print '***************************************************************'
    print '   Welcome to Micro User Information Management System'
    print '   [New User Register] Please press "r" on your keyboard!'
    print '   [Old User Login] Please press "l" on your keyboard!'
    print '   [View All Users] Please press "v" on your keyboard!'
    print '   [Delete User] Please press "d" on your keyboard!'
    print '   [Change Password] Please press "c" on your keyboard!'
    print '   [Quit System] Please press "q" on your keyboard!'
    print '***************************************************************'
    myShow()


以上是关于微型用户信息管理系统MUIMS(Micro User Information Management System)V1.0的主要内容,如果未能解决你的问题,请参考以下文章

微型用户信息管理系统MUIMS(Micro User Information Management System)V1.0

微型用户信息管理系统MUIMS(Micro User Information Management System)V1.0

基于Zephyr在微型MCU上使用Tensor Flow Lite Micro做图像分类

微惯性导航系统

微型学生信息管理系统的建立

Caliburn.Micro 能否很好地与用户控件配合使用?