Now that you've gotten your feet wet with enterprise-style document management programming, look at the second open-source project of the chapter, OpenLDAP and the python-ldap module.
As mentioned earlier, OpenLDAP is a directory service that comes in a packaged form. It's a convenient package because it runs well under Linux or Windows, and it implements the standard LDAP protocol. LDAP in general is a very solid, well-understood protocol for directory access, and it's the long-term strategy for many key computer platforms, including Microsoft's Active Directory, Sun's SunOne directory server, and offerings from other vendors as well. LDAP, as an infrastructure component, is not going to go away soon, and using it from Python is incredibly simple.
When working through the LDAP examples, you have two options. You can use an existing LDAP server in your organization to try scripts out on (in this case, you'll need to modify the scripts to match your server's schema, authentication, and other values that will already be defined for you), or you can set up a test LDAP server, load some data into it, and use that to work with. The first Try It Out that follows explains how to set up your own LDAP server for these examples, if that's the way you want to go, and it's a good exercise to help you understand a little more about how LDAP works.
Unfortunately, although there are high-quality precompiled Windows distributions for the OpenLDAP server software itself, there are currently no recent Windows builds of the client software used by the python-ldap modules, and no interfaces from python-ldap to the standard Microsoft LDAP client in wldap32.dll. Therefore, for all of these LDAP examples, as of the time this book is published, you'll need to have a Linux or Unix system to build an OpenLDAP instance. Fortunately, if you are a Windows user, you can use the cygwin toolkit to create an environment that you can use to build and run OpenLDAP. For instructions on downloading and installing OpenLDAP and the python-ldap module, see the web site for this book.
1. After you've downloaded and installed an OpenLDAP package and have followed this book's web site instructions for how to set up a basic server, make sure that the domain name it serves in the slapd.conf file is "wftk.org" if you want to use the following examples without modifying them. When OpenLDAP is running on your system, use a text editor to create the following LDIF file anywhere you want:
# Add a simple, generic user dn: cn=Different Person,dc=wftk,dc=org objectClass: person sn: Different Person cn: Different Person
# Add another user dn: cn=Michael Roberts,dc=wftk,dc=org objectClass: person sn: Roberts cn: Michael Roberts
# Add a workflow group: wfstarter dn: cn=wfstarter,dc=wftk,dc=org objectclass: organizationalRole cn: wfstarter roleOccupant: cn=Michael Roberts roleOccupant: cn=Different Person</repository>
2. Save the file as testldif.txt, and then use ldapadd to add the data you just entered:
[[email protected] michael]$ ldapadd -x -D "cn=Manager,dc=wftk,dc=org" -W -f testldif.txt Enter LDAP Password:
adding new entry "cn=Different Person,dc=vivtek,dc=com" adding new entry "cn=Michael Roberts,dc=vivtek,dc=com" adding new entry "cn=wfstarter,dc=vivtek,dc=com"
3. Now, use ldapsearch to see what happened (note the command on the first line):
[[email protected] michael]$ ldapsearch -x -b 'dc=vivtek,dc=com' '(objectclass=*)'
# extended LDIF
# base <dc=wftk,dc=org> with scope sub
# requesting: ALL
# Different Person, wftk.org dn: cn=Different Person,dc=wftk,dc=org objectClass: person sn: Different Person cn: Different Person
# Michael Roberts, wftk.org dn: cn=Michael Roberts,dc=wftk,dc=org objectClass: person sn: Roberts cn: Michael Roberts
# wfstarter, wftk.org dn: cn=wfstarter,dc=wftk,dc=org objectClass: organizationalRole cn: wfstarter roleOccupant: cn=Michael Roberts roleOccupant: cn=Different Person
# search result search: 2 result: 0 Success
# numResponses: 4
# numEntries: 3
Was this article helpful?