7. Querying Oak LDAP from Python

The instructions in this section assume that you have configured your system according to Common Client Configuration.

You will need python-ldap (on Debian-based systems, aptitude install python-ldap should be sufficient).

You will also need to be running with access to Kerberos credentials (eg by running kinit as described in the introductory documentation above, or by setting the KRB5CCNAME environment variable to point to a suitable credentials cache).

Sample code:

   #!/usr/bin/python
   
   import ldap, ldap.sasl, sys, pprint
   
   try:
     auth = ldap.sasl.gssapi("")
     oakldap = ldap.initialize("ldap://ldap.oak.ox.ac.uk:389")
     oakldap.start_tls_s()
     oakldap.sasl_interactive_bind_s("",auth)
     results = oakldap.search_s( "ou=people,dc=oak,dc=ox,dc=ac,dc=uk",
                                 ldap.SCOPE_SUBTREE,
                                 "(sn=Howes)" )
   except ldap.LDAPError, error:
     print "LDAP error: %s" % error
     sys.exit()
   
   pp = pprint.PrettyPrinter()
   pp.pprint(results)

Up: Contents Previous: 6. Querying Oak LDAP from PHP Next: 8. Querying Oak LDAP from Java