LDAP Programming in Python

An introduction to the python-ldap package for writing applications.
Running the Program

You probably are anxious now to run the program and see it in action. But first, you need to append the following code to call the main() function. It should go at the end of your script before you run it.

if __name__=='__main__':

Now that you have inputted everything you need to run the Python program, save the source code in your editor and name the file ldap-test.py. Give it a run and test it out. If you don't see any output, it is probably because you didn't enter a valid LDAP server and/or who and cred, or because there simply is no existing search field on the server that matches the one we're using (in this case *ryan*).

Next Steps

We have only gotten our feet wet with how to use Python to write LDAP applications; you can do a lot more with python-ldap. You can find more python-ldap programming examples here.

For more information, check out the python-ldap documentation. A complete list of LDAP-related RFCs also are freely available on-line. If you are looking for a good book, consider LDAP Programming, Management, and Integration by Clayton Donely and LDAP: Programming Directory-Enabled Applications With Lightweight Directory Access Protocol by Tim Howes. These books use programming languages such as C/C++, Perl and Java for their code examples, but they still can be helpful for those who wish to code LDAP applications in Python. For an open-source implementation of LDAP, visit OpenLDAP's web site. There's also a developer's mailing list.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

thanks very much, I tried

Anonymous's picture

thanks very much, I tried your code, it works well. But when I learn it from someother ldap server, i found that i cann't use the base="", I must write the concrete base if i want it to work. I really want to know what is the problem. I googled for this question, failed to get the reson, could you help me. I am really confused about this: why you said, in this example: base can be "". fortunately, the first example I tried with "" is also ok. I am a fresh bird, can someone help me?? That will be very appreciated.

I have just start to learn python

Anonymous's picture

thanks for your share. i will have a try and learn.

I tried python for the first

Anonymous's picture

I tried python for the first time and noticed that tabbing cripples the whole python language. Thanks and goodbye


KOMTET's picture

We have translated your article in russian. If you have no objection - posted it on our website.

Quite poorly written. The

Anonymous's picture

Quite poorly written. The following is a much more practical explanation of getting started with python & ldap.


It was good for the time

Anonymous's picture

Keep in mind that in 2003, when this article was written, there were no other articles on LDAP programming in Python. So I think it was quite good for the time.

By the way, the linked code

Anonymous's picture

By the way, the linked code is terrible. It's like someone tried to adapt another language for python.

The search code uses a permanent TRUE statement with a break.

Bad code

Anonymous's picture

I was able to pick out the basics from this example, but the python code is terrible...

Re: LDAP Programming in Python

Anonymous's picture

The code, for the most part, works for me. But I'm having trouble slicing the data portions out of the search result.

When grabbing 'description' (or any other attribute), I'm getting back:

string indices must be integers " entry[1]['description'][0] "

Re: LDAP Programming in Python

Anonymous's picture

try this:

look for Python's Manual about List, Tuple and Dictionary!

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState