[python-ldap] Connect to multiple servers for failover

Prashant Bapat prashant at apigee.com
Mon Apr 20 18:02:46 CEST 2015


Ok. I wasn't able to get the flexibility I wanted with that approach.

Settled for something like this.

LDAPSERVERS = ["srv1", "srv2"]
for server in LDAPSERVERS:
    try:
        conn = ldap.initialize("ldap://%s" % (server))
        conn.start_tls_s()
        conn.simple_bind_s()
        response = conn.search_s(search ,ldap.SCOPE_BASE,
"(objectClass=*)", ["attr1", "attr2"])
        # If we get a response lets not go to the next server. Break here.
        if response:
            break
    except ldap.NO_SUCH_OBJECT:
        # We got a error and the queried username is wrong. Lets not bother
the other server.
        raise sys.exit(-1)
    except LDAPError, e:
        # Lets go to the next server.
        ldaperrors[server] = e
        continue

Thanks.
--Prashant

On 20 April 2015 at 21:26, Michael Ströder <michael at stroeder.com> wrote:

> Prashant Bapat wrote:
>
>> Is there a way to connect to multiple servers for a query and iteratively
>> try all of them before failing ?
>>
>> Can I do something like
>>
>> ldap.initialize(srv1, srv2, srv3) and it connects to srv2 if srv1 is down
>> etc
>>
>> Is this possible ?
>>
>
> Did not test thoroughly myself but parameter uri is simply passed to
> OpenLDAP's libldap and libldap takes several space-separated LDAP URIs.
>
> I prefer to deal with that one level higher though.
>
> Ciao, Michael.
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ldap/attachments/20150420/fecb25be/attachment.html>


More information about the python-ldap mailing list