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__':
    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.

______________________

Comments

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

Translation

KOMTET's picture

Great!
Thanks.
We have translated your article in russian. If you have no objection - posted it on our website.
http://www.komtet.ru/info/python/ldap-v-python

Quite poorly written. The

Anonymous's picture

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

http://www.grotan.com/ldap/python-ldap-samples.html

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:
entyr[0][1]['description']

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

White Paper
Fabric-Based Computing Enables Optimized Hyperscale Data Centers

Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.

Learn More

Sponsored by AMD

White Paper
Red Hat White Paper: Using an Open Source Framework to Catch the Bad Guy

Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6

Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.

Learn more about catching the bad guy in this free white paper.

Learn More

Sponsored by DLT Solutions