Python LDAP

Share

                      Python LDAP

Python provides several libraries to interact with LDAP (Lightweight Directory Access Protocol) servers. LDAP is commonly used to access and manage directory information services, such as Active Directory or OpenLDAP. Here are some popular Python libraries for working with LDAP:

  1. ldap3: ldap3 is a comprehensive, actively maintained library that supports a wide range of LDAP operations and is compatible with both Python 2 and Python 3. It provides a high-level API for LDAP operations and is easy to use. You can install it using pip:
bash
pip install ldap3

Here’s a simple example of how to use ldap3 to connect to an LDAP server and perform a search:

python

from ldap3 import Server, Connection, ALL

server = Server(‘ldap.example.com’, get_info=ALL)
conn = Connection(server, ‘username’, ‘password’, auto_bind=True)

conn.search('dc=example,dc=com', '(objectclass=*)', attributes=['cn', 'mail'])
for entry in conn.entries:
print(entry.cn, entry.mail)

  1. python-ldap: python-ldap is another popular library for LDAP operations in Python. It is a wrapper around the OpenLDAP C API and provides a lower-level interface compared to ldap3. While it’s powerful and efficient, it can be more complex to use. It is also compatible with Python 2 and Python 3.
bash
pip install python-ldap

Here’s a basic example using python-ldap:

python

import ldap

ldap_server = ‘ldap://ldap.example.com’
ldap_user = ‘username’
ldap_password = ‘password’

ldap_conn = ldap.initialize(ldap_server)
ldap_conn.simple_bind_s(ldap_user, ldap_password)

base_dn = ‘dc=example,dc=com’
search_filter = ‘(objectClass=*)’
attributes = [‘cn’, ‘mail’]

result = ldap_conn.search_s(base_dn, ldap.SCOPE_SUBTREE, search_filter, attributes)
for dn, entry in result:
print(entry['cn'][0], entry.get('mail', [''])[0])

Remember to replace 'ldap.example.com', 'username', 'password', 'dc=example,dc=com', and the search filter with appropriate values for your LDAP server.

Both libraries are capable of handling various LDAP operations like search, add, modify, delete, etc. Choose the one that best fits your requirements and preferences. For most use cases, ldap3 is recommended due to its ease of use and active development community.

Python Training Demo Day 1

 
You can find more information about Python in this Python Link

 

Conclusion:

Unogeeks is the No.1 IT Training Institute for Python  Training. Anyone Disagree? Please drop in a comment

You can check out our other latest blogs on Python here – Python Blogs

You can check out our Best In Class Python Training Details here – Python Training

💬 Follow & Connect with us:

———————————-

For Training inquiries:

Call/Whatsapp: +91 73960 33555

Mail us at: info@unogeeks.com

Our Website ➜ https://unogeeks.com

Follow us:

Instagram: https://www.instagram.com/unogeeks

Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute

Twitter: https://twitter.com/unogeeks


Share

Leave a Reply

Your email address will not be published. Required fields are marked *