Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
I have to create user authentication using python-ldap. After googling around and trying out stuffs, this is the final code for you to use. Please remember to adjust the user_dn, and base_dn accordingly to the format used in your LDAP server.
# to be able to import ldap run pip install python-ldap
import ldap
if __name__ == "__main__":
username = "someuser"
password= "somepassword"
# the following is the user_dn format provided by the ldap server
user_dn = "uid="+username+",ou=someou,dc=somedc,dc=local"
# adjust this to your base dn for searching
base_dn = "dc=somedc,dc=local"
connect =
search_filter = "uid="+username
#if authentication successful, get the full user data
result = connect.search_s(base_dn,ldap.SCOPE_SUBTREE,search_filter)
# return all user data results
print result
except ldap.LDAPError:
print "authentication error"
Copy link

bharatazmera commented Jul 17, 2018

I am getting connect =
AttributeError: 'module' object has no attribute 'open' this error. I have ldap module installed. I am not sure what is the error.
Can you help me, please?

Copy link

chandimsett commented Nov 5, 2018

Use connect = ldap.initialize(ldap_server)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment