我通常使用ConfigParser并将密码作为散列存储在文本文件中。然后我从文本文件中检索散列并在脚本中解码,然后将其作为Config对象的属性发送给任何需要它的对象。在
下面是一个config parser类的示例:import configparser
import os
cwd = os.path.dirname(__file__)
class UserEntity:
def __init__(self, user=None,
password=None):
config = configparser.ConfigParser()
config_file = 'users.cfg'
config_location = os.path.join(cwd, config_file)
with open(config_location, 'r') as cfg:
config.read_file(cfg)
if not user:
self.user = config['credentials']['user']
else:
self.user = user
if not password:
self.password = config['credentials']['password']
else:
self.password = password
usr = UserEntity()
print(usr.password, usr.user)
usr2 = UserEntity(user&