我想用python查詢LDAP服務器,它總是返回我沒有結果。任何人都可以幫我找到我的Python代碼有什麼問題?它運行良好,沒有任何異常,並且它總是沒有結果。我玩過像「cn = partofmyname」的過濾器,但沒有運氣。 感謝您的幫助使用Python的LDAP查詢:總是沒有結果
import ldap
try:
l = ldap.open("server")
l.protocol_version = ldap.VERSION3
l.set_option(ldap.OPT_REFERRALS, 0)
output =l.simple_bind("cn=username,cn=Users,dc=domian, dc=net",'password$R')
print output
except ldap.LDAPError, e:
print e
baseDN = "DC=domain,DC=net"
searchScope = ldap.SCOPE_SUBTREE
## retrieve all attributes - again adjust to your needs - see documentation for more options
retrieveAttributes = None
Filter = "(&(objectClass=user)(sAMAccountName=myaccount))"
try:
ldap_result_id = l.search(baseDN, searchScope, Filter, retrieveAttributes)
print ldap_result_id
result_set = []
while 1:
result_type, result_data = l.result(ldap_result_id, 0)
if len(result_data) == 0:
print 'no reslut'
break
else:
for i in range(len(result_set)):
for entry in result_set[i]:
try:
name = entry[1]['cn'][0]
email = entry[1]['mail'][0]
phone = entry[1]['telephonenumber'][0]
desc = entry[1]['description'][0]
count = count + 1
print "%d.\nName: %s\nDescription: %s\nE-mail: %s\nPhone: %s\n" %\
(count, name, desc, email, phone)
except:
pass
## here you don't have to append to a list
## you could do whatever you want with the individual entry
#if result_type == ldap.RES_SEARCH_ENTRY:
# result_set.append(result_data)
# print result_set
except ldap.LDAPError, e:
print e
l.unbind()
我不知道 - 由於某種原因,我之前的編輯首選「ReSlut」稱號http://stackoverflow.com/posts/3057039/revisions它讓我。 – 2010-06-16 20:48:38
如果我正確記得,python'ldap'模塊還可以打開/增加路由到std輸出的調試信息的詳細程度。 – ChristopheD 2010-06-16 21:19:05