3

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?

1
  • What is a valid connection? Commented Jan 31, 2014 at 11:38

2 Answers 2

8

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
Sign up to request clarification or add additional context in comments.

1 Comment

I found it usefull, thank you :) I have edited your answer and made the function return the connection object, which is usually needed later on ;)
1

Found a solution:

import ldap

try:
    ldapObject = ldap.open(host="host", port=389)
    ldapObject .simple_bind_s()
except ldap.SERVER_DOWN:
    print("Failed")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.