I did the ldap connection successfully. How to go:
1.I have python v 3.7.2
2.Install python-ldap:For this I tried "pip install python-ldap" but it not worked
for me on windows machine so I use the alternate below.
3.For installing ldap go here:https://www.lfd.uci.edu/~gohlke/pythonlibs/#python-ldap
and download python_ldap‑3.1.0‑cp37‑cp37m‑win_amd64.whl
4.Now move to the download directory and run "pip install
python_ldap‑3.1.0‑cp37‑cp37m‑win_amd64.whl"
- Now open python shell and check "import ldap" if you are getting no error means you are done.
This is the sample code:
#Resource of code :https://gist.github.com/ibeex/1288159
import ldap
def check_credentials(username, password):
"""Verifies credentials for username and password.
Returns None on success or a string describing the error on failure
# Adapt to your needs
"""
LDAP_SERVER = 'xxx'
# fully qualified AD user name
LDAP_USERNAME = '%[email protected]' % username
# your password
LDAP_PASSWORD = password
base_dn = 'DC=spi,DC=com'
ldap_filter = 'userPrincipalName=%[email protected]' % username
attrs = ['memberOf']
try:
# build a client
ldap_client = ldap.initialize(LDAP_SERVER)
# perform a synchronous bind
ldap_client.set_option(ldap.OPT_REFERRALS,0)
ldap_client.simple_bind_s(LDAP_USERNAME, LDAP_PASSWORD)
except ldap.INVALID_CREDENTIALS:
#print("wron")
ldap_client.unbind()
return 'Wrong username or password'
except ldap.SERVER_DOWN:
#print("down")
return 'AD server not awailable'
# all is well
# get all user groups and store it in cerrypy session for future use
ab = str(ldap_client.search_s(base_dn,
ldap.SCOPE_SUBTREE, ldap_filter, attrs)[0][1]['memberOf'])
#print("ab"+ab)
ldap_client.unbind()
return 'success'
if __name__ == "__main__":
u="chirag"
p="secred"
print(check_credentials(u,p))
!/usr/local/env python
, which runs whatever "python" would on the command line. – Lumbago