Python: How can I import a different file when the user enters the password correctly?

时间:2015-07-29 00:45:20

标签: python

Loading a different file after user enters password, then program runs. And if the user doesn't enter correctly the different file shall not run.

How do I accomplish this?

2 个答案:

答案 0 :(得分:1)

如果您确实想根据某些条件语句导入不同的文件,可以将import语句放在if块中:

passwd = getUserPassword()
isAuthenticated = checkUserPasswordCorrect(passwd)

if isAuthenticated == True:
    import moduleA as module
else:
    import moduleB as module

// assuming you want to call a function called 'myFunc' which
// is different in moduleA and moduleB
module.myFunc()

答案 1 :(得分:0)

首先,使用hashlib获取正确密码的哈希值,如此提前:

import hashlib
stored_pass_hash = hashlib.sha224("CorrectPass").hexdigest()

这将为您提供' 2c08bc4d0b631f761705fcb04c395c8ddeda74253531e9acd2d28d8e'这将很难破解。

使用该哈希,您可以安全地比较输入密码:

s = raw_input('Enter Your Password')

if hashlib.sha224(s).hexdigest() == stored_pass_hash:
    #Run correct code here
else:
    print "Bad Password"