Warm tip: This article is reproduced from serverfault.com, please click

ldap3 print false and empty list. Why is the list empty?

发布于 2020-12-01 09:12:44

I have now managed to establish a connection, but now only False and an empty list come out. I have cut the search_base. This is my first time working with ldap.

ldap.py:

import ldap3
from ldap3 import Server, Connection, ALL

s = 'ldap://...int'
user = 'Y'
password = '1234'

server = Server(s, get_info = ALL)
conn = Connection(server,user,password)
print(conn.bind())
result = conn.search(search_base='ou=...,dc=...,dc=...,dc=...,', search_filter='(initials=user)')
print(result)
print(conn.entries)

Output:

False
False
[]
Questioner
Ina Heike
Viewed
0
LisaJ 2020-12-02 12:08:54

Import LDAPBindError and wrap your bind attempt in a try/except. You should then get some indication if your connection or bind attempt aren't working (e.g. "ldap3.core.exceptions.LDAPSocketOpenError: invalid server address" if the directory server hostname is bad).

import ldap3
from ldap3 import Server, Connection, ALL
from ldap3.core.exceptions import LDAPBindError

server = Server('ldap://directoryserver.example.com', get_info = ALL)
con = Connection(server,'MyUser@example.com','P2s%w0rd')
try: 
    con.bind()
except LDAPBindError:
    print(con.result)