读取文件中的用户名和密码python中_从文件中提取存储的用户名和密码并由python脚本使用的有效方法是什么?...

我通常使用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&

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值