[lug] Connecting to MS LDAP server
Andrew Diederich
diederic at boulder.net
Fri Jul 18 12:59:36 MDT 2003
I wrote a python (1.5.2) script to put up a company phone list, connecting
to an Exchange 5.5 server. One thing that bit me is the default max
return value in exchange is 100, so you may need to bump it up. I think I
used 1.5.2 since I couldn't find an ldap module for 2.x. I've pasted the
script in below. I don't know if the tabs/whitespace will come through,
but here goes.
--
Andrew Diederich
diederic at boulder.net
#!/usr/bin/python
import ldap
print "Content-type: text/html"
print """
<HTML>
<HEAD>
<TITLE>Phone List</TITLE>
</HEAD>
<BODY LANG="en-US" BGCOLOR="#ccccff">
"""
EXCHANGE_SERVER = "exchange.server.address"
# l = ldap.open("my_ldap_server.my_domain", ldap_port)
l = ldap.open(EXCHANGE_SERVER)
l.simple_bind_s("","")
# Full result set
res = l.search_s( "", ldap.SCOPE_SUBTREE,
"objectclass=organizationalperson")
l.unbind()
# alphabetize the list
def mySort(a, b):
return cmp(a[1]['cn'], b[1]['cn'])
res.sort(mySort)
print """
<table border="1">
<tr><td><strong>Name</strong></td>
<td><strong>Phone</strong></td>
<td><strong>Mobile</strong></td>
<td><strong>Title</strong></td>
</tr>
"""
for user in res:
D = user[1]
if not D.has_key('telephoneNumber'): continue
if not D.has_key('title'): D['title'] = " "
if not D.has_key('mail'): D['mail'] = " "
if not D.has_key('mobile'): D['mobile'] = " "
# Below happens if a phone exists, but not a mail, and the has_key mail
thing
# isn't present
#Traceback (innermost last):
# File "./debugphonebook.py", line 65, in ?
# print '<td> %s </td>\n<td> %s </td>\n<td> %s </td>\n<td> %s
</td>\n</tr>\n' % (D['cn'][0], D['telephoneNumber'][0], D['mail'],
D['title'][0])
#KeyError: mail
# So that's why the above puts in a blank title or mail if none exists.
print '<tr>'
print '<td> %s </td>\n<td> %s </td>\n<td> %s </td>\n<td> %s
</td>\n</tr>\n' % (D['cn'][0], D['telephoneNumber'][0], D['mobile'][0],
D['title'][0])
print "</TABLE>"
from normalDate import ND
today = ND()
print "Today's date: "
print today.formatUS()
print "</BODY>"
print "</HTML>"
More information about the LUG
mailing list