The code segment gets a connection to an LDAP server. If no DN is given, the program tries to get a connection to the server using the current user's DN. If the given DN is not a valid LDAP directory node, an error is generated and the program ends.
from ldap3 import Server, Connection, ALL
def get_ldap_connection(dn=None, password=None, LDAP_URI=None):
server = Server(LDAP_URI, get_info=ALL)
conn = Connection(server, dn, password)
if not conn.bind():
print("Connon bind to ldap server")
print("bind to ldap server success")