Reputation: 13131
I want to be able to test that a connection to a host and port is valid.
I'm using the current line:
ldapObject = ldap.open(host="host", port=389)
This seems to return an instance. I need to determine if it can find the host or not?
Any suggestions?
Upvotes: 3
Views: 18468
Reputation: 274
open is deprecated. This is a working example. See if this helps.
def ldap_initialize(remote, port, user, password, use_ssl=False, timeout=None):
prefix = 'ldap'
if use_ssl is True:
prefix = 'ldaps'
# ask ldap to ignore certificate errors
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
if timeout:
ldap.set_option(ldap.OPT_NETWORK_TIMEOUT, timeout)
ldap.set_option(ldap.OPT_REFERRALS, ldap.OPT_OFF)
server = prefix + '://' + remote + ':' + '%s' % port
conn = ldap.initialize(server)
conn.simple_bind_s(user, password)
return conn
Upvotes: 7
Reputation: 13131
Found a solution:
import ldap
try:
ldapObject = ldap.open(host="host", port=389)
ldapObject .simple_bind_s()
except ldap.SERVER_DOWN:
print("Failed")
Upvotes: 1