Skip to content

Instantly share code, notes, and snippets.

@ozten
Created July 27, 2011 16:10
Show Gist options
  • Save ozten/1109709 to your computer and use it in GitHub Desktop.
Save ozten/1109709 to your computer and use it in GitHub Desktop.
Searching internal phonebook for country and manager
import ldap
from ldap.filter import filter_format
def search():
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
conn = ldap.initialize("ldaps://addressbook.mozilla.com:636")
try:
try:
o = conn.bind_s("[email protected],o=com,dc=mozilla", PASSWORD)
search_filter = filter_format("(cn=*%s*)", ("Austin", ))
attrs = None
try:
rs = conn.search_s("dc=mozilla", ldap.SCOPE_SUBTREE, search_filter, attrs)
except ldap.SIZELIMIT_EXCEEDED:
print("Too many results!")
if len(rs) > 0:
for result in rs:
dn, person = result
print("==== %s ====" % person['cn'])
if 'physicalDeliveryOfficeName' in person:
print(person['physicalDeliveryOfficeName'])
if 'manager' in person:
print(person['manager'])
else:
print("No results")
except ldap.INVALID_CREDENTIALS, ic:
print(ic)
finally:
conn.unbind()
search()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment