1

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.

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

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

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

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?

thanks in advance!

xmx
  • 137
  • 2
  • 14

1 Answers1

3

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)

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

Community
  • 1
  • 1
Thiago Almeida
  • 380
  • 2
  • 6
  • I assume I put that in my python script, which defines "user" and "pwd". What do I need to do to my existing python line that has the credentials hardcoded? Thanks! – xmx Apr 15 '17 at 02:54
  • You can use that: `l.simple_bind_s(user, pwd)` – Thiago Almeida Apr 15 '17 at 03:31
  • That worked beautifully!!! I will share entire script here later for everyone :-) thanks Thiago for all your help!! – xmx Apr 15 '17 at 04:36