active directory - Python LDAP: LDAPObject.search_s() works, but LDAPObject.search() doesn't -
i trying implement basic ldap authentication script in python, , trying perform simple ldap search , see if works. believe have correctly created , set ldap object , connection.
after binding, try perform search. using ldapobject.search_s() method returns list of strings user information. when use ldapobject.search() method, though, method returns result code 2, protocol error. reason want use search() method because returns int , not list. understand according the python ldap documentation, 2 methods can take in same arguments, don't understand why 1 method returning error , not other.
here code:
import ldap import getpass  # login info  username = raw_input("enter username: ") password = getpass.getpass("enter password: ")  ldap_server = "ldap://ipaddress:port" base_dn = "ou=domain users,dc=dummyname,dc=com" user_dn = username + "@dummyname.com" search_filter = "(&(objectclass=user)(samaccountname=" + username + "))"  ld = ldap.initialize(ldap_server); ld = ldap.open(ldap_server) ld.protocol_version = 3 ld.set_option(ldap.opt_referrals, 0)  # bind user information ldap connection  try:     print ld.simple_bind_s(user_dn, password)     results = ld.search(base_dn, ldap.scope_subtree, search_filter)     print results     ld.unbind_s() except ldap.invalid_credentials:     print "your username or password invalid." except exception e:     print("connection unsuccessful: " + str(e.message))     ld.unbind_s()   full output of code is:
enter username: myusername enter password: (97, [], 1, []) 2   any appreciated. thanks.
the following command using async search not block wait return value. int returns msgid of ldapobject need return value when returned.
msgid = ld.search(base_dn, ldap.scope_subtree, search_filter)  # returns int of msgid without blocking   to actual results need call
actual_results = ld.result(msgid)   # blocks until search done , returns [(dn,attrs)]   using following commands causes ldapobject search in "sequential order" blocking program.
results = ld.search_s(base_dn, ldap.scope_subtree, search_filter)  # blocks until results received   [(dn,attrs)]      
Comments
Post a Comment