Python从JSON文件中获取LDAP凭证

问题描述:

我在python脚本中有以下代码行,使用我的凭据绑定到LDAP(假设用户名是"abcdef",密码是"123456":

I have the following line of code in a python script to bind to LDAP using my credentials(assuming username is "abcdef", and password is "123456":

l.simple_bind_s("domain\abcdef", "123456")

当我运行脚本并执行查询时,效果很好.

Works fine when I run the script and do queries.

但是,我该如何替换要从JSON文件读取的硬编码凭据?

However, how can I replace the hardcoded credentials to be read from a JSON file instead?

我当前编写了这个JSON文件,并将其命名为creds.json:

I currently wrote this JSON file and named it creds.json:

{
    "username": "domain\\abcdef",
    "password": "123456"
}

我是编码新手,因此能够将凭据导入到存储在json文件中的python脚本中的任何帮助都将非常有用.我需要对python脚本做些什么才能使它工作?

Im new to coding, and any help would be great to be able to import the credentials to my python script that are stored in a json file. What would i need to do to the python script to make this work?

提前谢谢!

这是有关如何从json文件中获取值的简单示例.

That's a minimalist example of how to get values from a json file.

import json

with open('creds.json') as data_file:
    data = json.load(data_file)


user = data['username']
pwd =  data['password']

print(user)
print(pwd)

我从以下答案中得出: https://stackoverflow.com/a/2835672/4172067

I take from this answer: https://stackoverflow.com/a/2835672/4172067