Attachment 'my_search.py'

Download

   1 #!/usr/bin/env python
   2 
   3 import ldap
   4 
   5 def main():
   6     # It looks something like this: 
   7     server = "localhost"
   8     who = "cn=Manager,dc=example,dc=com"
   9     cred = "secret"
  10 
  11 
  12     #Now we need to make a keyword set to what we want our search string to be. I use my first name for this sample program:
  13     keyword = "Donald Duck"
  14 
  15     #Next, we need to bind to the LDAP server. Doing so creates an object named "l" that is then used throughout the program.
  16     try:
  17         los = ldap.open(server)
  18         los.simple_bind_s(who, cred)
  19         print "Successfully bound to server.\n"
  20 
  21 
  22         #We're now ready to query the server. We also now catch any possible errors if there is a problem authenticating:
  23         print "Searching..\n"
  24         my_search(los, keyword)
  25     except ldap.LDAPError, error_message:
  26         print "Couldn't Connect. %s " % error_message
  27 
  28 
  29 def my_search(los, keyword):
  30 
  31     #In a moment we will be calling python-ldap's built-in search method on our l object. Four variables--base, scope, filter and retrieve_attributes--are the parameters of that search method. Base is used for the DN (distinguished name) of the entry where the search should start. You can leave it blank for this example:
  32     base = "ou=People,dc=example,dc=com"
  33 
  34     #For scope we use SCOPE_SUBTREE to search the object and all its descendants:
  35     scope = ldap.SCOPE_SUBTREE
  36 
  37     #Our search filter consists of a cn (common name) and our keyword. Putting asterisks around our keyword (ryan) will match anything with the string ryan, such as Bryant.
  38     filter_s = "cn=" + keyword
  39 
  40 
  41     #The last argument we pass to the search method is used to return all the attributes of each entry:
  42     retrieve_attributes = None
  43 
  44     #Now, let's setup a few more variables, including a counter to keep track of the number of results returned:
  45     count = 0
  46 
  47     #a list to append the results to:
  48     result_set = []
  49 
  50     #and a variable to specify the length, in seconds, we're willing to wait for a response from the server:
  51     timeout = 0
  52 
  53     #Now we can begin our search by calling python-ldap's search method on our l object:
  54     try:
  55         result_id = los.search(base, scope, filter_s, retrieve_attributes)
  56 
  57         #Store any results in the result_set list
  58         while 1:
  59             result_type, result_data = los.result(result_id, timeout)
  60             if (result_data == []):
  61                 break
  62             else:
  63                 if result_type == ldap.RES_SEARCH_ENTRY:
  64                     result_set.append(result_data)
  65 
  66 
  67         #If we were to print result_set now, it might look like a big list of tuples and dicts. Instead, step through it and select only the data we want to see
  68         if len(result_set) == 0:
  69             print "No Results."
  70             return
  71         for i in range(len(result_set)):
  72             for entry in result_set[i]:
  73                 print entry
  74                 try:
  75                     name = entry[1]['cn'][0]
  76                     email = entry[1]['mail'][0]
  77                     phone = entry[1]['telephonenumber'][0]
  78                     desc = entry[1]['description'][0]
  79                     count = count + 1
  80 
  81 
  82                     #Display the data, if any was found, in the following format:
  83                     #1.    Name:    Description:    E-mail:    Phone:    2.    Name:    Description:    E-mail:    Phone:    etc..
  84                     print "%d.\nName: %s\nDescription: %s\nE-mail: %s\nPhone: %s\n" %\
  85                            (count, name, desc, email, phone)
  86                 except:
  87                     pass
  88 
  89     except ldap.LDAPError, error_message:
  90         print error_message
  91 
  92 
  93 #Running the Program
  94 #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.
  95 if __name__ == '__main__':
  96     main()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2008-06-28 12:08:22, 8.1 KB) [[attachment:ArnicaTestSeite.htm]]
  • [get | view] (2008-05-09 17:05:26, 1.3 KB) [[attachment:AttachFile_config_patch.patch]]
  • [get | view] (2010-11-28 20:20:00, 237.6 KB) [[attachment:Bildschirmfoto.png]]
  • [get | view] (2010-03-15 23:32:08, 1.4 KB) [[attachment:DictColumns.zip]]
  • [get | view] (2012-02-05 12:57:40, 1812.4 KB) [[attachment:HelpOnArnica.zip]]
  • [get | view] (2008-07-17 19:33:36, 3264.0 KB) [[attachment:MoinMoins_GHOP_Grand_Prize_Winner.mpg]]
  • [get | view] (2009-06-22 22:46:07, 490.1 KB) [[attachment:New_street_station_to_Conservatoire_to_premier_inn.png]]
  • [get | view] (2009-06-22 22:59:17, 70.6 KB) [[attachment:New_street_station_to_Conservatoire_to_premier_inn_weg.png]]
  • [get | view] (2009-09-04 11:37:10, 3.4 KB) [[attachment:SlideShow.patch]]
  • [get | view] (2010-02-02 18:27:19, 293.8 KB) [[attachment:Solenoid_svg_edit.png]]
  • [get | view] (2009-04-13 17:51:57, 22.8 KB) [[attachment:SystemPages.png]]
  • [get | view] (2009-04-13 18:58:14, 1.4 KB) [[attachment:SystemPages.py]]
  • [get | view] (2008-10-19 04:19:34, 252.4 KB) [[attachment:TheSheep1.png]]
  • [get | view] (2008-07-28 08:12:08, 685.8 KB) [[attachment:UnderConstruction.zip]]
  • [get | view] (2008-06-28 18:54:51, 7.9 KB) [[attachment:UnifyParsersAndMacros.zip]]
  • [get | view] (2007-06-07 16:02:03, 0.3 KB) [[attachment:abc.png]]
  • [get | view] (2009-09-06 11:39:34, 10.0 KB) [[attachment:abc235.adraw]]
  • [get | view] (2008-12-22 09:09:36, 1.1 KB) [[attachment:advanced_s.patch]]
  • [get | view] (2008-09-15 07:51:42, 2.0 KB) [[attachment:align1.png]]
  • [get | view] (2008-06-24 01:17:08, 255.3 KB) [[attachment:arnica_example1.png]]
  • [get | view] (2008-06-22 19:38:31, 128.2 KB) [[attachment:arnica_example2.png]]
  • [get | view] (2008-06-22 19:38:47, 83.4 KB) [[attachment:arnica_example3.png]]
  • [get | view] (2008-06-28 12:14:10, 38.9 KB) [[attachment:arnica_example5.png]]
  • [get | view] (2009-08-31 22:33:27, 22.3 KB) [[attachment:attachfile.patch]]
  • [get | view] (2009-08-31 22:36:36, 96.8 KB) [[attachment:attachfile.png]]
  • [get | view] (2009-09-04 11:39:45, 11.7 KB) [[attachment:background_1024x768.png]]
  • [get | view] (2009-10-05 10:49:10, 7.2 KB) [[attachment:c.png]]
  • [get | view] (2010-12-15 19:05:12, 5.7 KB) [[attachment:collapsed.png]]
  • [get | view] (2010-12-15 19:11:15, 20.1 KB) [[attachment:collapsed1.png]]
  • [get | view] (2010-12-16 08:10:18, 20.2 KB) [[attachment:collapsed2.png]]
  • [get | view] (2009-08-08 10:43:37, 48.2 KB) [[attachment:coverage_2.0.txt]]
  • [get | view] (2008-05-17 11:49:27, 1.9 KB) [[attachment:dicts_change.patch]]
  • [get | view] (2010-01-10 13:11:22, 14.3 KB) [[attachment:editmoin]]
  • [get | view] (2008-11-09 19:21:14, 6.0 KB) [[attachment:example.svg]]
  • [get | view] (2009-05-12 17:17:50, 33.5 KB) [[attachment:example_swf.png]]
  • [get | view] (2008-01-15 14:29:43, 243.9 KB) [[attachment:firefox_pdf.png]]
  • [get | view] (2009-10-11 14:11:41, 61.2 KB) [[attachment:kscan_0004.png]]
  • [get | view] (2008-12-30 17:50:02, 5.1 KB) [[attachment:latex2wiki.py]]
  • [get | view] (2008-03-21 17:04:38, 1.0 KB) [[attachment:maketestwiki.patch]]
  • [get | view] (2007-02-24 11:18:51, 1.4 KB) [[attachment:minipage.py]]
  • [get | view] (2009-08-08 16:16:13, 20.1 KB) [[attachment:moin-1.9-xapian-dmilajevs.txt]]
  • [get | view] (2009-08-08 22:55:55, 60.7 KB) [[attachment:moin-1.9.txt]]
  • [get | view] (2009-08-09 22:33:56, 29.2 KB) [[attachment:moin-2.0-storage.txt]]
  • [get | view] (2008-09-26 12:49:52, 684.7 KB) [[attachment:moinapi_beispiele.zip]]
  • [get | view] (2012-02-21 21:10:00, 37.7 KB) [[attachment:moinmoin_rb.png]]
  • [get | view] (2008-05-18 10:03:02, 3.8 KB) [[attachment:my_search.py]]
  • [get | view] (2009-09-07 13:00:22, 11.9 KB) [[attachment:navibar.png]]
  • [get | view] (2009-02-03 19:36:33, 4.2 KB) [[attachment:nbsp.jpg]]
  • [get | view] (2010-10-20 17:07:23, 318.5 KB) [[attachment:night_at_N1.jpg]]
  • [get | view] (2009-08-07 07:28:05, 736.1 KB) [[attachment:output.txt]]
  • [get | view] (2010-03-09 23:12:49, 0.6 KB) [[attachment:planning.zip]]
  • [get | view] (2010-08-14 09:50:12, 556.6 KB) [[attachment:preliminary_minefield.txt]]
  • [get | view] (2010-12-16 08:41:41, 66.8 KB) [[attachment:quicklinks1.png]]
  • [get | view] (2010-12-16 08:41:58, 117.3 KB) [[attachment:quicklinks2.png]]
  • [get | view] (2010-12-18 21:21:00, 13.1 KB) [[attachment:quicklinks3.png]]
  • [get | view] (2009-08-11 22:38:30, 221.4 KB) [[attachment:search.txt]]
  • [get | view] (2008-05-18 09:46:53, 1.6 KB) [[attachment:slapd.conf]]
  • [get | view] (2010-12-02 16:34:36, 123.0 KB) [[attachment:sunset.png]]
  • [get | view] (2009-09-23 15:12:58, 111.7 KB) [[attachment:svg-edit_ie8_chromeframe.png]]
  • [get | view] (2009-08-23 22:38:40, 43.4 KB) [[attachment:svg-editor_current_css.png]]
  • [get | view] (2009-04-14 21:22:58, 1.8 KB) [[attachment:test_strings.py]]
  • [get | view] (2009-01-06 00:11:59, 1.7 KB) [[attachment:text_gedit.patch]]
  • [get | view] (2009-07-12 10:51:27, 528.3 KB) [[attachment:the_end.jpg]]
  • [get | view] (2010-12-21 20:24:32, 22.3 KB) [[attachment:toctest1.png]]
  • [get | view] (2010-12-01 07:37:26, 186.8 KB) [[attachment:transcluded_video.png]]
  • [get | view] (2010-12-01 07:29:11, 264.0 KB) [[attachment:urlproblem.png]]
  • [get | view] (2010-11-28 23:56:54, 57.2 KB) [[attachment:wanted.png]]
  • [get | view] (2008-05-18 09:45:08, 3.4 KB) [[attachment:wikiconfig_snippet.txt]]
  • [get | view] (2009-08-09 18:00:32, 3.6 KB) [[attachment:xapian.txt]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.