0
我试图获取以逗号分隔的LDAP数据库中存储的所有电子邮件地址的列表。将数组转换为连接字符串的逗号分隔的脚本
通过简化这个script我:
#!/usr/bin/env python
# encoding: utf-8
# Author: Zhang Huangbin <zhb _at_ iredmail.org>
# Purpose: Add enabledService=lib-storage for all mail users.
# Required by IMAP folder sharing in Dovecot-2.0.
# Date: 2012-05-18
import sys
import ldap
# Note:
# * bind_dn must have write privilege on LDAP server.
uri = 'ldap://127.0.0.1:389'
basedn = 'o=domains,dc=example,dc=com'
bind_dn = 'cn=Manager,dc=example,dc=com'
bind_pw = 'password'
# Initialize LDAP connection.
print >> sys.stderr, "* Connecting to LDAP server: %s" % uri
conn = ldap.initialize(uri=uri, trace_level=0,)
conn.bind_s(bind_dn, bind_pw)
# Get all mail users.
print >> sys.stderr, "* Get all mail accounts..."
allUsers = conn.search_s(
basedn,
ldap.SCOPE_SUBTREE,
"(objectClass=mailUser)",
['mail', 'enabledService'],
)
total = len(allUsers)
print >> sys.stderr, "* Total %d user(s)." % (total)
# Counter.
count = 1
for user in allUsers:
(dn, entry) = user
mail = entry['mail'][0]
print >> "%s, " % (mail)
count += 1
# Unbind connection.
conn.unbind()
当我运行此我得到错误:
- Connecting to LDAP server: ldap://127.0.0.1:389
- Get all mail accounts...
- Total 64 user(s). Traceback (most recent call last): File "list_mail_users.py", line 43, in print >> "%s, " % (mail) AttributeError: 'str' object has no attribute 'write'
我问这个问题上的支持论坛和他们建议我使用:ldapsearch的呢?
此外,我希望你可能想'''.join(邮件)作为打印什么,而不是“%s”, – Perkins