Skip to content

Instantly share code, notes, and snippets.

@westfeld
Created May 28, 2019 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westfeld/bb1d5e8727ce13910623933e041e9782 to your computer and use it in GitHub Desktop.
Save westfeld/bb1d5e8727ce13910623933e041e9782 to your computer and use it in GitHub Desktop.
ldaptor test using Python 3.6.6 and twisted 19.2.0 and ldaptor 19.0.0
from twisted.internet import reactor, defer
from ldaptor.protocols.ldap import ldapclient, ldapsyntax, ldapconnector
@defer.inlineCallbacks
def example():
# The following arguments may be also specified as unicode strings
# but it is recommended to use byte strings for ldaptor objects
serverip = b'ldap.server.com'
basedn = b'dc=my-company,dc=com'
binddn = b'LDAP_USERNAME'
bindpw = b'LDAP_PASSWORD'
query = b'(cn=Joe)'
c = ldapconnector.LDAPClientCreator(reactor, ldapclient.LDAPClient)
overrides = {basedn: (serverip, 389)}
client = yield c.connect(basedn, overrides=overrides)
# startTLS fails
resp = yield client.startTLS()
yield client.bind(binddn, bindpw)
o = ldapsyntax.LDAPEntry(client, basedn)
results = yield o.search(filterText=query)
for entry in results:
print(entry.getLDIF())
if __name__ == '__main__':
df = example()
df.addErrback(lambda err: err.printTraceback())
df.addCallback(lambda _: reactor.stop())
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment