~ / archivo / stackoverflow / 64560955
Unable to set TIMEOUT for ldap in Python 2.7
-
I'd like to setup a "timeout" for the ldap library (python-ldap-2.4.15-2.el7.x86_64) and python 2.7
I'm forcing my /etc/hosts to resolve an non existing IP address in order to raise the timeout.
I've followed several examples and took a look at the documentation or questions like this one without luck.
By now I've tried forcing global timeouts before the initialize:
ldap.set_option(ldap.OPT_NETWORK_TIMEOUT, 1) ldap.set_option(ldap.OPT_TIMEOUT, 1) ldap.protocol_version=ldap.VERSION3Forced the same values at object level:
ldap_obj = ldap.initialize("ldap://%s:389" % LDAPSERVER ,trace_level=9) ldap_obj.set_option(ldap.OPT_NETWORK_TIMEOUT, 1) ldap_obj.set_option(ldap.OPT_TIMEOUT, 1)I've also tried with:
ldap.network_timeout = 1 ldap.timelimit = 1And used both methods, search and search_st (The synchronous form with timeout)
Finally this is the code:
def testLDAPConex( LDAPSERVER ) : """ Check ldap """ ldap.set_option(ldap.OPT_NETWORK_TIMEOUT, 1) ldap.set_option(ldap.OPT_TIMEOUT, 1) ldap.protocol_version=ldap.VERSION3 ldap.network_timeout = 1 ldap.timelimit = 1 try: ldap_obj = ldap.initialize("ldap://%s:389" % LDAPSERVER ,trace_level=9) ldap_result_id = ldap_obj.search("ou=people,c=this,o=place", ldap.SCOPE_SUBTREE, "cn=user")I've printed the constants of the object
OPT_NETWORK_TIMEOUTandOPT_TIMEOUT, the values were assigned correctly.The execution time is 56s everytime and I'm not able to control the amount of seconds for the timeout.
Btw, the same code in python3 does work as intended:
real 0m10,094s user 0m0,072s sys 0m0,013s -
After some testing I've decided to rollback the VM to a previous state.
The Networking Team were doing changes across the network configuration, that might have changed the behaviour over the interface and some kind of bug when the packages were being sent over the wire in:
ldap_result_id = ldap_obj.search("ou=people,c=this,o=place", ldap.SCOPE_SUBTREE, "cn=user")After restoring the VM, which included a restart, the error of not being able to reach LDAP raises as expected.