【问题标题】:Python, password for databasePython,数据库密码
【发布时间】:2016-05-09 03:29:10
【问题描述】:

我正在为类做简单的数据库,但密码功能有问题。我不明白为什么每次都要求设置密码。我认为问题在于阅读是否已经在基础中,但我不知道如何解决它。

import shelve
global base,pas
global magazine_base
def load_data():
    global base
    global magazine_base
    magazine_base = shelve.open('magazine_base')
    if magazine_base.has_key('base'):
        base = magazine_base['base']
        if not base:
            base = []
    else:
        base = []
        magazine_base['base'] = base

def enterpass():
    global base,pas
    epas=raw_input("Enter password to acces database")
    for entry in base:
        while True:
            if epas == entry['pas']:

                break
            else:
                print "Password incorrect"  

def password():
    global base,pas
    global magazine_base
    load_data()

    is_firstopen = True

    if 'pas' in base:
        is_firstopen = False
        enterpass()
    if is_firstopen:
        while True:
            pas=raw_input("This is the first start of database.\nPlease enter the password, min. 5 characters: ")
            if len(pas)>5:
                break
                base += [{'pas':pas}]
                magazine_base['base'] = base
                magazine_base.close()
                print "Password set"
            else:
                print "Password too short"

【问题讨论】:

    标签: python database passwords


    【解决方案1】:

    在无限循环中提示您输入密码。

    【讨论】:

    • 我将其简化为: if 'pas' in base: enterpass() else: print "This is the first start of database.\n Please enter the password, min. 5 characters: " while True: pas=raw_input() if len(pas)>5: break base += [{'pas':pas}] magazine_base['base'] = base magazine_base.close() print "Password set" else: print "Password too简而言之,请再次输入:“但还是一样,可能 pass 没有被读取或正确保存
    【解决方案2】:

    我们来看看这个函数:

    def enterpass():
        global base,pas
        epas=raw_input("Enter password to acces database")
        for entry in base:
            while True:
                if epas == entry['pas']:
                    break
                else:
                    print "Password incorrect"  
    

    显然,base 是一个包含条目的列表。每个条目将是一个具有键“pas”的字典。如果“pas”条目与用户密码匹配,则密码有效。我认为如果没有匹配密码是错误的。这不是代码的作用,而是试试这个:

    import sys
    
    def enter_password(base):
        """Prompt the user for a password, and validate it against the 
        passwords stored in the entries in base. Permit three tries 
        before terminating the application."""
    
        from getpass import getpass
    
        for tries in range(3):
            userpw = getpass("Enter password to access database: ")
    
            for entry in base:
                if entry['pas'] == userpw:
                    return
    
            print "\nInvalid password!\n"
    
        print "\nToo many failed password attempts. Goodbye!"
        sys.exit(1)
    

    你可以这样称呼它:

    enter_password(base)
    

    【讨论】:

    • 对不起,我不知道如何在我的代码中实现这一点,我正在尝试,但我收到了一些关于全局变量和不同变量的错误。无论如何我不明白从哪里需要正确的密码,如何设置密码
    • 我也不知道从哪里得到正确的密码!我正在查看您的代码,假设您知道密码存储在 base 数据中。 (getpass 调用从用户那里收集密码,但应该与什么进行比较?)
    猜你喜欢
    • 2016-12-04
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 2022-10-19
    • 2015-11-12
    • 2023-04-08
    • 1970-01-01
    相关资源
    最近更新 更多